android启动服务的生命周期,Android Service的两种启动方式以及生命周期

Service的两种启动方式:

1.startService

2.bindService

注意:

.在Android 5.0之后google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛个异常出来.

1.startService的启动代码和销毁代码:

0818b9ca8b590ca3270a3433284dd417.png

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()

0818b9ca8b590ca3270a3433284dd417.png

bindService启动销毁的log

0818b9ca8b590ca3270a3433284dd417.png01-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

0818b9ca8b590ca3270a3433284dd417.png01-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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值