Service的两种启动方式:
1.startService 2.bindService注意:.在Android 5.0之后google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛个异常出来.
1.startService的启动代码和销毁代码:
public class ListViewActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View.inflate(ListViewActivity.this, R.layout.activity_list_view, contentView);
ButterKnife.bind(this);
Intent intent=new Intent(ListViewActivity.this, ExampleService.class);
startService(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
Intent serviceIntent = new Intent(this,ExampleService.class);
stopService(serviceIntent);
}
}
2. bindService启动代码
public class RxJavaActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_java);
/* Observable<String> myObservable=Observable.create{
}*/
Intent gattServiceIntent = new Intent(this, ExampleService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
private final ServiceConnection mServiceConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
}
}
注意:OnDestroy方法中需要写 unbindService(mServiceConnection)负责,log层会报错,但是对程序无影响,不写退出Activity也会销毁这个Service。
Service:
public class ExampleService extends Service {
int mStartMode;
IBinder mBinder;
boolean mAllowRebind;
public ExampleService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("ExampleService","onCreate()");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("ExampleService", "onStart()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ExampleService", "onStartCommand()");
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.d("ExampleService", "onBind()");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("ExampleService", "onUnbind()");
return mAllowRebind;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ExampleService", "onDestroy()");
}
}
startService启动销毁log
01-28 20:44:56.901 2047-2047/com.langtian.matchlayout D/ExampleService: onCreate()
01-28 20:44:57.025 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:44:57.029 2047-2047/com.langtian.matchlayout D/ExampleService: onStartCommand()
01-28 20:45:13.881 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:45:14.781 2047-2047/com.langtian.matchlayout D/ExampleService: onDestroy()
bindService启动销毁的log
01-28 20:46:03.373 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:46:03.897 2047-2047/com.langtian.matchlayout I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread.
01-28 20:46:13.461 2047-2047/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:46:14.161 2047-2047/com.langtian.matchlayout I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
01-28 20:46:15.205 2047-2047/com.langtian.matchlayout D/ExampleService: onUnbind()
01-28 20:46:15.205 2047-2047/com.langtian.matchlayout D/ExampleService: onDestroy()
startService启动退出时不销毁,再bindService启动的log
01-28 20:47:46.157 3905-3905/com.langtian.matchlayout D/ExampleService: onCreate()
01-28 20:47:46.157 3905-3905/com.langtian.matchlayout D/ExampleService: onStartCommand()
01-28 20:47:48.621 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:50.229 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:50.229 3905-3905/com.langtian.matchlayout D/ExampleService: onBind()
01-28 20:47:50.813 3905-3905/com.langtian.matchlayout I/Choreographer: Skipped 35 frames! The application may be doing too much work on its main thread.
01-28 20:47:53.441 3905-3905/com.langtian.matchlayout W/EGL_genymotion: eglSurfaceAttrib not implemented
01-28 20:47:54.145 3905-3905/com.langtian.matchlayout I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
01-28 20:47:55.345 3905-3905/com.langtian.matchlayout D/ExampleService: onUnbind()
注意:启动bindService没有再执行OnCreate方法,所以onCreate方法只执行一次。
服务运行中,只有onStartCommand()可多次调用,其他在一个生命周期只调用一次。
startService的生命周期 onCreate()------>onStartCommand------->running----->stopService()/stopSelf()----->onDestroy()
bindService的生命周期
onCreate()------->onBind()------->running------>onUnbind()------->onDestroy()
先启动startService后启动BindService的生命周期
onCreate()------>onStartCommand------->running------->onBind()------->running------>onUnbind()------->onDestroy()