android service莫名启动问题

  当您发现service莫名启动,则说明该service已经死了,又借尸还魂了。它的pid已经发生变化了。这是为什么呢?

这是因为:该service在此之前已经被kill掉了。而service在被杀后,在有些情况下是可以复活的,具体什么情况下复活,稍后会讲。

如果您发现service借尸还魂现象,请到android/service.log中查看,您会发现:

01-04 13:18:24.720   434   692 I ActivityManager: No longer want com.ophone.reader.ui:memory_resident (pid 745): hidden #16
这样的Log。这说明手机处于低内存状态,你的进程被LMK干掉了。

解决办法:

如果您不想service被杀,这里有个办法:

在AndroidManifest.xml中application中加入android:persistent="true"

如:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:persistent="true"

强制修改进程属性:
在程序中设置setPersistent(true);
在project的AndroidManifest.xml的<application>中加入属性android:persistent="true"
android:persistent(SDK)
    Whether or not the application should remain running at all times . The default value is "false". Applications should not normally set this flag; persistence mode is intended only for certain system applications(phone,system).
 
1.使用killProcess (int pid)可以杀死指定PID的进程
public void onClick(View v) {                 
   android.os.Process.killProcess(android.os.Process.myPid());
}

然后把您的apk push 到/system/app下就自动安装了了,保证不会被LMK杀了,但是不能保证被kernel的OOM被杀哦。

如果您对进程被杀无所谓,但是不想它重启,这里也有个办法,先介绍点背景知识。

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,问题解决。








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值