Service 莫名启动解决方案

最近项目中遇到一个问题:程序在系统注册了broadcastreceiver来监听启动通知,在onReceive中启动了一个service通 过service的onStart来启动程序。问题在于,程序退出时没有stop这个service,之后当系统正常运行一段时间(1-2小时),程序突然自动启动,查看log后发现这样一句:

start proc ****  for service *****

可以发现这个service被系统kill后一段时间内有重新被创建运行了,造成程序再次自动运行。因此,查阅一部分资料后做一些总结。


一 service的生命周期


二 service运行方式

如果使用startService()启动service,系统将通过传入的Intent在底层搜索相关符合Intent里面信息的 service。如果服务没有启动则先运行onCreate,然后运行onStartCommand (可在里面处理启动时传过来的Intent和其他参数),直到明显调用stopService或者stopSelf才将停止Service。无论运行 startService多少次,只要调用一次stopService或者stopSelf,Service都会停止。使用stopSelf(int)方 法可以保证在处理好intent后再停止。


onStartCommand 的原型为 public int onStartCommand(Intent intent, int flags, int startId) ,在2.0后被引入用于service的启动函数,2.0之前为public void onStart(Intent intent, int startId) 。

这里onStartCommand 函数返回一个整形值:

1、START_STICKY

2、START_NOT_STICKY

3、START_REDELIVER_INTENT


对于1的解释:
Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don’t retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

在运行onStartCommand后service进程被kill后,那将保留在开始状态,但是不保留那些传入的intent。不久后 service就会再次尝试重新创建,因为保留在开始状态,在创建 service后将保证调用onstartCommand。如果没有传递任何开始命令给service,那将获取到null的intent。


对于2的解释:
Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don’t recreate until a future explicit call to Context.startService(Intent). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.

在运行onStartCommand后service进程被kill后,并且没有新的intent传递给它。Service将移出开始状态,并且直 到新的明显的方法(startService)调用才重新创建。因为如果没有传递任何未决定的intent那么service是不会启动,也就是期间 onstartCommand不会接收到任何null的intent。


对于3的解释:
Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).

在运行onStartCommand后service进程被kill后,系统将会再次启动service,并传入最后一个intent给 onstartCommand。直到调用stopSelf(int)才停止传递intent。如果在被kill后还有未处理好的intent,那被 kill后服务还是会自动启动。因此onstartCommand不会接收到任何null的intent。

当service运行在低内存的环境时,将会kill掉一些存在的进程。因此进程的优先级将会很重要,可以使用startForeground API将service放到前台状态。这样在低内存时被kill的几率更低,但是如果在极度极度低内存的压力下,该service还是会被kill掉。

至此,已经找到了问题根源,重写了service的onStartCommand函数,返回值设定为START_NOT_STICKY,问题解决。

另外,Service不是线程,这意味着它将在主线程里工作,所以如果里面有处理耗时间的任务可以将耗时的任务移至新线程中处理,或者用系统提供的IntentService,它继承了Service,它处理数据是用自身新开的线程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值