一、Service第一种启动方式startService
生命周期:oncreate——>onstartCommand——>onDestroy;多次通过该方法启动Service,oncreate函数只会被调用一次,onStartCommand函数会被多次调用,但只需要调用一次stopService就可以销毁该Service;
特点说明:通过该启动方式启动Service,一旦Service启动成功那么该Service就会和他所创建的Activity脱离关系,也就是说Service的不会随着Activity的销毁而销毁,并且在应用的任何Activiy中都可以对该Service进行操作。通过以下demo进行说明
(1)创建一个Service类,并在生命周期方法中输出日志:
public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); Log.i("MyService", "onCreate: "); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("MyService", "onStartCommand: "); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.i("MyService", "onDestroy: "); } }(2)在 SecondActivity中通过StartService的方式启动,输出结果为:
(3)退出SecondActivity,在MainActivity中再次启动Service,输出结果为:
09-01 10:57:47.729 28261-28261/com.example.mvp.communication I/MyService: onStartCommand:
(4)重新进入到SecondActivity结束Service,输出结果为:
对比上面结果可以很容易得出:Service在被一个Activity启动之后,会脱离它所启动的Activity而存在。
二、Service第二种启动方式bindService
生命周期:oncreate——>onBind——>unBind——>OnDestroy;多次bindService,oncreate和onBind方法都只会调用一次,而且只需要一次调用unBindService用于取消bind;
特点说明:它与startService正好相反,一个Activity通过bind的方式启动Service,那么该Service是依赖于该Activity存在的,也就是Activity的销毁也会导致bind所启动的Service的销毁(会经历unbind——>onDestroy的生命周期);相关demo如下:
(1)创建Service,并在Service中定义一个内部类同时继承Binder,并定义方法test(),该方法可在调用bindService的Activity中进行调用:
public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); Log.i("MyService", "onCreate: "); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("MyService", "onStartCommand: "); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.i("MyService", "onDestroy: "); } @Override public IBinder onBind(Intent intent) { Log.i("MyService", "onBind: "); return new MyBinder(); } @Override public boolean onUnbind(Intent intent) { Log.i("MyService", "onUnbind: "); return super.onUnbind(intent); } class MyBinder extends Binder { public void test() { Log.i("MyService", "test: "); } }
(2)在SecondActivity中通过bind的方式启动Service:
public class SecondActivity extends AppCompatActivity { private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { ((MyService.MyBinder) binder).test(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } public void click(View view) { Intent intent = new Intent(this, MyService.class); switch (view.getId()) { case R.id.bind: bindService(intent, connection, BIND_AUTO_CREATE); break; case R.id.unbind: unbindService(connection); break; } } }
三、既通过startService启动也通过bindService的方式进行启动
同时通过这两种方式进行启动,oncreate方法只会调用一次,在需要销毁Service的时候,需要同时调用stopService和unBindService(没有先后顺序)。