Android-服务Service(2)-bind绑定Service及两种生命周期
2017-05-10 11:23
567 查看
上篇是第一种方法,这里主要是第二种Context.bindService()方法
首先对于对于bindService()Method,
参数如下:
各个参数解释如下:
1.service:即通过intent指定的服务,与之前第一种方法一致
2.conn:这是ServiceConnection的一个对象,其中有两个 函数,分别是onServiceConnected()和onServiceDisconnected(),用来监听访问者与Service之间的连接情况,当访问者与Service成功连接将回调onServiceConnected函数,当Service进程被crashed or killed(异常终止等)则回调onServiceDisconnected函数。
具体函数为:
3.flag:即绑定时是否自动创建Service,如上参数图所示,0为不自动,BIND_AUTO_CREATE为自动创建
测试demo
MainActivity:
MyService:
UI:
测试:
当按下绑定服务:
首先自动创建服务(flag那设置的),回调onCreate()函数,
之后绑定服务,回调onBind()函数
最后回调ServiceConnection里面的onServiceConnection函数
之后按下解绑服务
回调服务里面onUnbind()函数
之后回调服务里面onDestroy()函数
startService后,再绑定服务。当解绑服务时候,Service并没有回调onDestroy()函数,
而绑定服务再解绑时,则会调用onDestroy()函数。
这是因为第一种情况,Service是由startService创建的,不是bindService。而第二种情况,则是bindService创建的Service。从这里发现,当bindService时,如果服务已经创建,那么解绑只是解除访问者与Service的绑定,而Service并不会终止。
这里关于生命周期,调用方法不同,周期也不同。
调用startService周期:onCreate()-onStartCommand()-onDestroy()
调用bindService周期:onCreate()-onBind()-Unbind()-onDestroy()
相关链接
Android-服务Service(1)-介绍及startService调用
Android-服务Service(2)-bind绑定Service及两种生命周期
Android-服务Service(3)-IntentService
1. 开发步骤
1. 绑定服务
Intent intent=new Intent(getBaseContext(),MyService.class); bindService(intent,connection, Context.BIND_AUTO_CREATE);
2. 解绑服务
unbindService(connection);
首先对于对于bindService()Method,
参数如下:
各个参数解释如下:
1.service:即通过intent指定的服务,与之前第一种方法一致
2.conn:这是ServiceConnection的一个对象,其中有两个 函数,分别是onServiceConnected()和onServiceDisconnected(),用来监听访问者与Service之间的连接情况,当访问者与Service成功连接将回调onServiceConnected函数,当Service进程被crashed or killed(异常终止等)则回调onServiceDisconnected函数。
具体函数为:
ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // method } @Override public void onServiceDisconnected(ComponentName name) { // method } };
3.flag:即绑定时是否自动创建Service,如上参数图所示,0为不自动,BIND_AUTO_CREATE为自动创建
测试demo
MainActivity:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Intent intent=new Intent(getBaseContext(),MyService.class); Button btstart=(Button)findViewById(R.id.button1); Button btstop=(Button)findViewById(R.id.button2); Button btbind=(Button)findViewById(R.id.button3); Button btunbind=(Button)findViewById(R.id.button4); btstart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //启动服务 startService(intent); } }); btstop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //停止服务 stopService(intent); } }); btbind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bindService(intent,connection, Context.BIND_AUTO_CREATE); } }); btunbind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unbindService(connection); } }); }//onCreate ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // method System.out.println("onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { // method System.out.println("onServiceDisconnected"); } }; }
MyService:
public class MyService extends Service { public MyService() { } //绑定时调用 @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. System.out.println("Service Binded"); // throw new UnsupportedOperationException("Not yet implemented"); return new Binder(); } // System.out 用于测试 //创建时调用 public void onCreate(){ super.onCreate(); System.out.println("Service Created"); Toast.makeText(this,"Service Created",Toast.LENGTH_LONG).show(); } //Service 启动时调用 public int onStartCommand(Intent intent,int flags,int startID){ System.out.println("Service Started"); Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show(); return START_STICKY; } //关闭时调用 public void onDestroy(){ super.onDestroy(); System.out.println("Service Destroyed"); Toast.makeText(this,"Service Destroyed",Toast.LENGTH_SHORT).show(); } //解除绑定时调用 public boolean onUnbind(Intent intent){ System.out.println("Service Unbinded"); return true; } }
UI:
测试:
当按下绑定服务:
首先自动创建服务(flag那设置的),回调onCreate()函数,
之后绑定服务,回调onBind()函数
最后回调ServiceConnection里面的onServiceConnection函数
之后按下解绑服务
回调服务里面onUnbind()函数
之后回调服务里面onDestroy()函数
2. 关于生命周期
在这里可以清晰看到,startService后,再绑定服务。当解绑服务时候,Service并没有回调onDestroy()函数,
而绑定服务再解绑时,则会调用onDestroy()函数。
这是因为第一种情况,Service是由startService创建的,不是bindService。而第二种情况,则是bindService创建的Service。从这里发现,当bindService时,如果服务已经创建,那么解绑只是解除访问者与Service的绑定,而Service并不会终止。
这里关于生命周期,调用方法不同,周期也不同。
调用startService周期:onCreate()-onStartCommand()-onDestroy()
调用bindService周期:onCreate()-onBind()-Unbind()-onDestroy()
相关链接
Android-服务Service(1)-介绍及startService调用
Android-服务Service(2)-bind绑定Service及两种生命周期
Android-服务Service(3)-IntentService
相关文章推荐
- Android应用程序绑定服务(bindService)的过程源代码分析
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
- Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用
- Android服务Service详解(作用,生命周期,AIDL)系列文章--服务绑定和开启混合使用
- Android应用程序绑定服务(bindService)的过程源代码分析(2)
- Android应用程序绑定服务(bindService)的过程
- Android系统在新进程中启动自定义服务过程(startService)的原理分析、Android应用程序绑定服务(bindService)的过程源代码分析
- Android Service学习之 Service生命周期 & 绑定类型服务& Binder类
- Android服务Service详解(作用,生命周期,AIDL)系列文章--绑定服务
- Android应用程序绑定服务(bindService)的过程源代码分析(1)
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- Android中服务(Service)的的生命周期,两种开启服务的方式的不同点,Service与Activity之间的通讯
- Android服务Service详解(作用,生命周期,AIDL)系列文章--进一步封装绑定服务(私有化代理类)
- Android应用程序绑定服务(bindService)的过程源代码分析
- Android应用程序绑定服务(bindService)的过程源代码分析(3)
- Android应用程序绑定服务(bindService)的过程源码分析
- Android 服务(service)的生命周期以及利用bindservice调用服务里面的方法
- Android应用程序绑定服务(bindService)的过程源代码分析
- Android Service服务的生命周期