Service的两种启动方式:
1.startService
2.bindService
注意:
.在Android 5.0之后google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛个异常出来.
1.startService的启动代码和销毁代码:
public classListViewActivity extendsBaseActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View.inflate(ListViewActivity.this, R.layout.activity_list_view, contentView);
ButterKnife.bind(this);
Intent intent=newIntent(ListViewActivity.this, ExampleService.class);
startService(intent);
}
@Overrideprotected voidonDestroy() {
super.onDestroy();
Intent serviceIntent = newIntent(this,ExampleService.class);
stopService(serviceIntent);
}
}
2. bindService启动代码
public classRxJavaActivity extendsBaseActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_java);
/* Observable myObservable=Observable.create{}*/Intent gattServiceIntent = newIntent(this, ExampleService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
private finalServiceConnection mServiceConnection= newServiceConnection(){
@Overridepublic voidonServiceConnected(ComponentName name, IBinder service) {
}
@Overridepublic voidonServiceDisconnected(ComponentName name) {
}
};
@Overrideprotected voidonDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
}
}
注意:OnDestroy方法中需要写
unbindService(
mServiceConnection
)负责,log层会报错,但是对程序无影响,不写退出Activity也会销毁这个Service。
Service:
public classExampleService extendsService {
intmStartMode;
IBinder mBinder;
booleanmAllowRebind;
publicExampleService() {
}
@Overridepublic voidonCreate() {
super.onCreate();
Log.d("ExampleService","onCreate()");
}
@Overridepublic voidonStart(Intent intent, intstartId) {
super.onStart(intent, startId);
Log.d("ExampleService", "onStart()");
}
@Overridepublic intonStartCommand(Intent intent, intflags, intstartId) {
Log.d("ExampleService", "onStartCommand()");
returnmStartMode;
}
@OverridepublicIBinder onBind(Intent intent) {
//TODO: Return the communication channel to the service.Log.d("ExampleService", "onBind()");
returnmBinder;
}
@Overridepublic booleanonUnbind(Intent intent) {
Log.d("ExampleService", "onUnbind()");
returnmAllowRebind;
}
@Overridepublic voidonDestroy() {
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()