安卓服务(Service)的两种开启方式以及服务的生命周期
2015-03-19 11:58
579 查看
安卓中服务的开启方式
一:采用start的方式开启服务调用函数:startService(Intent)->onCreate()->onStart()/onStartCommand()->onDestroy()
特点:服务被开启后不会重复开启,只会调用onStart(),服务只会被停止一次。
二:采用bind的方式开发服务
调用函数:bindService(Intent…)->onCreate()->onBind()->onUnBind()->onDestroy();
特点:绑定不会调用onStart()[过时了]和onStartCommand()方法。
两种服务的区别:
start方式开发服务,一旦服务开启跟调用者就没有任何关系了,比如我们的服务是在Activity中调用开启的,当Activity关闭的时候,服务不会关闭。
bind方式开启服务,调用者没了,服务也会关闭,可以理解为同生共死。
对于start开启服务的方式比较简单,重点讲解bind的方式!
例子:
1.布局里面设置三个按钮
2.为按钮设置监听事件,有好几种方式。
3.处理事件。当点击绑定的时候:
/*绑定的创建方式 */ Intent intent = new Intent(this, MyService.class); bindService(intent, conn, BIND_AUTO_CREATE);
conn是自己实现的功能类,继承自ServiceConnection。
完整代码如下:
public class MainActivity extends ActionBarActivity { private MyConn conn; private Call c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//Call类似中间人的功能。 } public void bind(View v){ conn = new MyConn(); Intent intent = new Intent(this, MyService.class); bindService(intent, conn, BIND_AUTO_CREATE); } public void unbind(View v){ unbindService(conn); c = null; } /* * 调用服务里的方法 */ public void call(View v){ c.callMethodInService(); } private class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("调用服务里面的方法"); c = (Call) service; } @Override public void onServiceDisconnected(ComponentName name) { } } }
service代码如下:
public class MyService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("服务被成功绑定了"); return new Call(); } @Override public void onCreate() { System.out.println("onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onstartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { System.out.println("ondestroy"); super.onDestroy(); } public void methodInService(){ Toast.makeText(this, "methodInService", 0).show(); } public class Call extends Binder{ public void callMethodInService(){ methodInService(); } } }
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.servicelife.MainActivity" > <Button android:onClick="bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start"/> <Button android:onClick="unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="end"/> <Button android:onClick="call" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用服务里的方法"/> </LinearLayout>
点击下载源代码
相关文章推荐
- 安卓服务(Service)的两种开启方式以及服务的生命周期
- Android中服务(Service)的的生命周期,两种开启服务的方式的不同点,Service与Activity之间的通讯
- Service学习(本地服务的两种启动方式以及跨进程服务)
- Activity生命周期以及Intent两种画面跳转方式
- java学习11--线程创建的两种方式,生命周期以及守护线程
- Service的分类以及不同种类的服务在生命周期中如何执行
- .服务生命周期,开启方式,绑定服务,远程服务
- 安卓非绑定方式的service生命周期测试
- (一)Service的生命周期和两种启动方式
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- Android服务Service详解(作用,生命周期,AIDL)系列文章--服务绑定和开启混合使用
- Android Service生命周期,本地服务远程服务,前台服务后台服务,启动方式,与IntentService区别全面解析
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- Servlet访问路径的两种方式、Servlet生命周期特点、计算服务启动后的访问次数、Get请求、Post请求
- 安卓Service的开启方式
- Windows 编程中用Service开启一个外部进程的两种语言实现方式(C++,C#)。
- 安卓Service绑定方式启动服务并调用服务中的方法
- Activity生命周期的应用场景举例以及显隐式Activity启动的两种方式和启动系统常见的ACtivity
- 安卓四大组件之二#1-Service的创建,生命周期以及动态调用Service里的方法 推荐
- Service两种启动方式的生命周期