1.Context.startService()方式启动
①Context.startService()方式的生命周期: 启动时,startService –> onCreate() –> onStart()停止时,stopService –> onDestroy()如果调用者直接退出而没有停止Service,则Service 会一直在后台运行 Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法附代码
2.Context.bindService()方式启动: ①Context.bindService()方式的生命周期: 绑定时,bindService -> onCreate() –> onBind()调用者退出了,即解绑定时,Srevice就会unbindService –>onUnbind() –> onDestory()Context.bindService()方式启动 Service的方法:绑定Service需要三个参数:bindService(intent, conn, Service.BIND_AUTO_CREATE);第一个:Intent对象第二个:ServiceConnection对象,创建该对象要实现它的onServiceConnected()和 onServiceDisconnected()来判断连接成功或者是断开连接第三个:如何创建Service,一般指定绑定的时候自动创建附代码
package com.dada.test; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Button; import com.dada.test.BindService.MyBinder; public class TestActivity extends Activity { private boolean flag; private static final String TAG = "TestActivity" ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); super .onCreate(savedInstanceState); setContentView(R.layout.main); Button btnStart = (Button) findViewById(R.id.btnStart); Button btnStop = (Button) findViewById(R.id.btnStop); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bindService(); } }); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { unBindService(); } }); } private void bindService(){ Intent intent = new Intent(TestActivity. this ,BindService. class ); Log.i(TAG, "bindService()" ); bindService(intent, conn, Context.BIND_AUTO_CREATE); } private void unBindService(){ Log.i(TAG, "unBindService() start...." ); if (flag == true ){ Log.i(TAG, "unBindService() flag" ); unbindService(conn); flag = false ; } } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "onServiceDisconnected()" ); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "onServiceConnected()" ); MyBinder binder = (MyBinder)service; BindService bindService = binder.getService1(); bindService.MyMethod(); flag = true ; } }; }
service
package com.dada.test; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindService extends Service { private static final String TAG = "BindService" ; private MyBinder myBinder = new MyBinder(); public void MyMethod(){ Log.i(TAG, "BindService-->MyMethod()" ); } @Override public IBinder onBind(Intent intent) { Log.i(TAG, "BindService-->onBind()" ); return myBinder; } public class MyBinder extends Binder{ public BindService getService1(){ return BindService. this ; } } @Override public void onCreate() { Log.i(TAG, "BindService-->onCreate()" ); super .onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.i(TAG, "BindService-->onStart()" ); super .onStart(intent, startId); } @Override public void onDestroy() { Log.i(TAG, "BindService-->onDestroy()" ); super .onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, "BindService-->onUnbind()" ); return super .onUnbind(intent); } }
运行日志
点击启动
点击停止
没有打出onServiceDisconnected的日志的原因:
注:SDK上是这么说的:This is called when the connection with the service has been unexpectedly disconnected -- that is, its process crashed. Because it is running in our same process, we should never see this happen.
所以说,只有在service因异常而断开连接的时候,这个方法才会用到
其他
由于Service 的onStart()方法只有在startService()启动Service的情况下才调用,故使用onStart()的时候要注意这点。
与 Service 通信并且让它持续运行
如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。