开启一个服务有两种方法,第一种为startSevice,第二种则是bindService。
一、startSevice方式
Service代码如下:
public class MyService extends Service { @Override public void onCreate(){ super.onCreate(); Log.v("tag","服务:onCreate"); } @Override public void onDestroy(){ super.onCreate(); Log.v("tag","服务:onDestroy"); } @Override public int onStartCommand(Intent intent,int flags,int startId){ Log.v("tag","服务:onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onRebind(Intent it){ super.onRebind(it); Log.v("tag","服务:onRebind"); } @Override public boolean onUnbind(Intent it){ Log.v("tag","服务:onUnbind"); return super.onUnbind(it); } public class IBinderImpl extends Binder{ public MyService getInstance(){ return MyService.this; } }; @Override public IBinder onBind(Intent intent) { Log.v("tag","服务:onBind"); return new IBinderImpl(); } }
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); startService(it); //bindService(it,conn,Service.BIND_AUTO_CREATE); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); Main.this.stopService(it); } });
1.运行程序
2.开启服务,连续点击按钮1三次
3.按home键
4.重新回到程序
5.点击按钮2关闭服务
6.再次点击按钮1开启服务
7.按返回键
Log日志打印状况如下:
12-17 23:07:20.981: V/tag(1492): Activity:onCreate
12-17 23:07:21.021: V/tag(1492): Activity:onStart
12-17 23:07:21.021: V/tag(1492): Activity:onResume
12-17 23:07:48.301: V/tag(1492): 服务:onCreate
12-17 23:07:48.332: V/tag(1492): 服务:onStartCommand
12-17 23:07:50.781: V/tag(1492): 服务:onStartCommand
12-17 23:07:51.820: V/tag(1492): 服务:onStartCommand
12-17 23:08:00.281: V/tag(1492): Activity:onPause
12-17 23:08:03.582: V/tag(1492): Activity:onStop
12-17 23:08:12.582: V/tag(1492): Activity:onRestart
12-17 23:08:12.582: V/tag(1492): Activity:onStart
12-17 23:08:12.600: V/tag(1492): Activity:onResume
12-17 23:08:31.290: V/tag(1492): 服务:onDestroy
12-17 23:09:10.331: V/tag(1492): 服务:onCreate
12-17 23:09:10.351: V/tag(1492): 服务:onStartCommand
12-17 23:09:23.881: V/tag(1492): Activity:onPause
12-17 23:09:25.020: V/tag(1492): Activity:onStop
12-17 23:09:25.020: V/tag(1492): Activity:onDestroy
日志分析结果如下:
1.运行程序
Activity:onCreate-->onStart-->onResume
服务:
2.开启服务,连续点击按钮1三次
Activity:
服务:onCreate-->onStartCommand-->onStartCommand-->onStartCommand
说明服务运行后,在这种方式下再次开启服务不会重新创建,只会重复调用onStartCommand
3.按home键
Activity:onPause-->onStop
服务:
4.重新回到程序
Activity:onRestart-->onStart-->onResume
服务:
说明home键不会停止服务。5.点击按钮2关闭服务
Activity:
服务:onDestory
6.再次点击按钮1开启服务
Activity:
服务:onCreate-->onStartCommand
7.按返回键
Activity:onPuase-->onStop-->onDestory
服务:
说明在这种模式下,活动退出后不影响服务的生命周期,服务依然存在。
二、bindService方式
服务代码略有变动:
public class MyService extends Service { @Override public void onCreate(){ super.onCreate(); Log.v("tag","服务:onCreate"); } @Override public void onDestroy(){ super.onCreate(); Log.v("tag","服务:onDestroy"); } @Override public int onStartCommand(Intent intent,int flags,int startId){ Log.v("tag","服务:onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onRebind(Intent it){ super.onRebind(it); Log.v("tag","服务:onRebind"); } @Override public boolean onUnbind(Intent it){ Log.v("tag","服务:onUnbind"); return super.onUnbind(it); } public class IBinderImpl extends Binder{ public MyService getInstance(){ Log.v("tag","IBinder接口的实例对象里的方法被调用了"); return MyService.this; } }; @Override public IBinder onBind(Intent intent) { Log.v("tag","服务:onBind"); return new IBinderImpl(); } }
先定义服务通道,
private MyService service; public ServiceConnection conn=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.v("tag","onServiceConnected"+",ComponentName:"+name); Main.this.service=((MyService.IBinderImpl)service).getInstance(); } @Override public void onServiceDisconnected(ComponentName name) { Log.v("tag","onServiceDisconnected"); Main.this.service=null; } };
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); bindService(it,conn,Service.BIND_AUTO_CREATE); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(service!=null){ unbindService(conn); service=null; } } });
12-18 00:27:04.211: V/tag(1773): Activity:onCreate
12-18 00:27:04.211: V/tag(1773): Activity:onStart
12-18 00:27:04.220: V/tag(1773): Activity:onResume
12-18 00:27:14.661: V/tag(1773): 服务:onCreate
12-18 00:27:14.670: V/tag(1773): 服务:onBind
12-18 00:27:14.741: V/tag(1773): onServiceConnected,ComponentName:ComponentInfo{com.example.testservice/com.example.testservice.MyService}
12-18 00:27:14.760: V/tag(1773): IBinder接口的实例对象里的方法被调用了
12-18 00:28:27.881: V/tag(1773): 服务:onUnbind
12-18 00:28:27.881: V/tag(1773): 服务:onDestroy
12-18 00:28:31.050: V/tag(1773): 服务:onCreate
12-18 00:28:31.050: V/tag(1773): 服务:onBind
12-18 00:28:31.100: V/tag(1773): onServiceConnected,ComponentName:ComponentInfo{com.example.testservice/com.example.testservice.MyService}
12-18 00:28:31.112: V/tag(1773): IBinder接口的实例对象里的方法被调用了
12-18 00:28:36.231: V/tag(1773): Activity:onPause
12-18 00:28:37.670: V/tag(1773): Activity:onStop
12-18 00:28:37.701: V/tag(1773): Activity:onDestroy
12-18 00:28:37.741: V/tag(1773): 服务:onUnbind
12-18 00:28:37.751: V/tag(1773): 服务:onDestroy
有关Activity的周期就不用说了,这里bindService的周期如下:
1.开启服务
onCreate-->onBind-->onServiceConnected-->IBinder接口的实例对象里的方法被调用了
说明bindService运行时ServiceConnection对象会自动获取onBind返回的IBider实例,获取了这个实例后则可调用该类中(服务中的方法)的方法。
2.再次开启服务
没有任何反映
3.关闭服务
onUnbind-->onDestroy
4.重新开启服务
onCreate-->onBind-->onServiceConnected-->IBinder接口的实例对象里的方法被调用了
5.按返回键
首先活动销毁,然后服务onUnbind-->onDestroy
这里与startService方式最大的区别就是,bindService中service的生命周期与activity相同。
代码下载:
三、最后测试一下onRebind的调用时机。
上面服务代码不变,按钮事件变为如下三个,分别为绑定,重绑,退出:
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); startService(it); bindService(it,conn,Service.BIND_AUTO_CREATE); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); bindService(it,conn,Service.BIND_AUTO_CREATE); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it=new Intent(Main.this,MyService.class); service.stopService(it); Main.this.finish(); } });
总结,onRebind方法指明:当内存中已有service时,当调用bindService时会调用onRebind(前提是onUnbind必须返回true).
补充:而不得不提的是,当我将进程kill掉后,日志打印出了"服务:onCreate,服务:onStartCommand",也就说当服务意外终止时自动又创建了服务,所以当获取服务对象为null时,可以先bindSerivice获取IBinder的实例。这其实是因为创建服务时用了Service.BIND_AUTO_CREATE参数。
上述三个例子的代码,路过的朋友有需要可以在此下载:http://download.csdn.net/detail/kkkkkxiaofei/6734441