Service中onStartCommand返回值
1.START_STICKY
表示Service运行的进程被Android系统强制杀掉之后,Android系统会将该Service依然设置为started状态(即运行状态),但是不再保存onStartCommand方法传入的intent对象,然后Android系统会尝试再次重新创建该Service,并执行onStartCommand回调方法,但是onStartCommand回调方法的Intent参数为null,也就是onStartCommand方法虽然会执行但是获取不到intent信息。
1)第一次启动service:
onCreate()====>
intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>1
2)杀死进程
onCreate()====>
onStartCommand() intent====>null flags===>0 startId===>3
3)进入
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>4
4)杀死进程
onCreate()====>
onStartCommand() intent====>null flags===>0 startId===>6
5)进入
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>7
2.START_NOT_STICKY
示当Service运行的进程被Android系统强制杀掉之后,不会重新创建该Service,当然如果在其被杀掉之后一段时间又调用了startService,那么该Service又将被实例化。
1)第一次启动service
onCreate()====>
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>1
2)杀死进程
没任何log
3)进入
onCreate()====>
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>1
3.START_REDELIVER_INTENT
表示Service运行的进程被Android系统强制杀掉之后,与返回START_STICKY的情况类似,Android系统会将再次重新创建该Service,并执行onStartCommand回调方法,但是不同的是,Android系统会再次将Service在被杀掉之前最后一次传入onStartCommand方法中的Intent再次保留下来并再次传入到重新创建后的Service的onStartCommand方法中,这样我们就能读取到intent参数。只要返回START_REDELIVER_INTENT,那么onStartCommand重的intent一定不是null。
1)第一次启动service
onCreate()====>
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>1
2)杀死进程
没任何log
3)进入
onCreate()====>
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>1 startId===>1
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>3
4.START_STICKY_COMPATIBILITY
1)第一次启动service
onCreate()====>
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>1
2)杀死进程
onCreate()====>
onStartCommand() intent====>null flags===>0 startId===>3
3)进入
onStartCommand() intent====>Intent { cmp=com.smilexie.weekview.myapplication/.TestService } flags===>0 startId===>4