总结一下本地的service的启动方式

来源:互联网 发布:淘宝深圳美版s7 编辑:程序博客网 时间:2024/06/09 16:08

服务(service)是android四大组件之一!一般用来执行不提供用户交互界面的操作,例如:下载、播放音乐。
大致可以划分为本地service和远程service!
1.本地service: 就是和当前应用在同一个进程中的service
2.远程service: 不同进程间的service访问,不同的进程间无法使用一般的方式共享数据。主要通过AIDL接口实现。(本篇暂不总结)
第一,本地service。
主要有两种启动方式:

一.非绑定模式

       startService(Intent intent):启动一个service 
       stopService(Intent intent) :停止一个service 

·绑定模式

bindService(Intent intent, ServiceConnection conn, int flags)

unbindService(ServiceConnection conn); 


先看看非绑定模式

先贴上Service 的代码:

public class MainService extends Service {private static final String TAG = "MainService";@Overridepublic void onCreate() {Log.i(TAG, "MainService-onCreate");  super.onCreate(); }@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "MainService-onStartCommand");  return super.onStartCommand(intent, flags, startId);  }@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(TAG, "MainService-onBind");  return null;}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "MainService-onUnbind");  return super.onUnbind(intent);}@Overridepublic void onRebind(Intent intent) {Log.i(TAG, "MainService-onRebind");  super.onRebind(intent); }@Override  public void onDestroy() {  Log.i(TAG, "MainService-onDestroy");  super.onDestroy();  }  }

下面是点击启动的代码(Activity界面)

statrBtn = (Button) this.findViewById(R.id.start_btn);statrBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubLog.i("MainService", "MainActivity click start"); Intent mIntent = new Intent(MainActivity.this, MainService.class);startService(mIntent);}});stopBtn = (Button) this.findViewById(R.id.stop_btn);stopBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {Log.i("MainService", "MainActivity click stop"); // TODO Auto-generated method stubIntent mIntent = new Intent(MainActivity.this, MainService.class);stopService(mIntent);}<span style="white-space:pre"></span>}
);

当第一次点击statrBtn 的时候,Mainservice 的 onCreate 和onStartCommand 都做了打印

当再一次点击statrBtn Mainservice 的onCreate没有被执行,只执行了onStartCommand 

点击 stopBtn MainserviceonDestroy 被执行!

再点击statrBtn 的时候 Mainservice的 onCreate 和 onStartCommand 又重新做了执行!

甚至按back键退出Activity,重新再回来Activity,点击statrBtn,也只执行onStartCommand,说明startService并没有绑定Activity和Mainservice


再试试绑定模式

先贴上BindService 的代码:

public class BindService extends Service {private static final String TAG = "BindService";public void BindMethod(){Log.i(TAG, "BindService-->BindMethod()");}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(TAG, "BindService-onUnbind");  return myBinder;}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "BindService-onUnbind");  return super.onUnbind(intent);}@Overridepublic void onRebind(Intent intent) {Log.i(TAG, "BindService-onRebind");  super.onRebind(intent); }@Override  public void onDestroy() {  Log.i(TAG, "BindService-onDestroy");  super.onDestroy();  }  public class MyBinder extends Binder{public BindService getService(){return BindService.this;}}private MyBinder myBinder = new MyBinder();}

注意,跟MainService不同的是 里面定义了一个Binder!

Activity的代码

先定义一个

ServiceConnection 

private boolean connFlag;private BindService bindService;private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.i("MainService", " onServiceDisconnected"); }@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubMyBinder binder = (MyBinder)service;bindService = binder.getService();bindService.BindMethod();connFlag = true;}};

bindBtn = (Button) this.findViewById(R.id.bind_btn);bindBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this,BindService.class);bindService(intent, conn, Context.BIND_AUTO_CREATE);}});unbindBtn = (Button) this.findViewById(R.id.unbind_btn);unbindBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(connFlag){unbindService(conn);connFlag = false;}}});

注意:在Activity的文件是通过onServiceConnected里获得的 bindService = binder.getService();

再通过bindService 去调用 BindService 提供的方法比如bindService.BindMethod();,达到调用的目的!


第一次点击绑定:进入onCreate和onBind 并且直接进入Activity定义的
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

}
一旦绑定成功后再次或多次点击绑定没有任何作用,不进入任何的生命周期


 
点击解锁 进入onUnbind和onDestroy注意解锁的时候不会进入Activity定义的
@Override
public void onServiceDisconnected(ComponentName name) {}

它的调用时机是当Service服务被异外销毁时,例如内存的资源不足时这个方法才被自动调用。


当绑定之后,退出该Activity的时候,onUnbind和onDestroy就会被自动调用解锁


就是这样子了!呵呵

0 0