当您发现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,问题解决。

android service莫名启动问题相关推荐

  1. android服务的启动过程,Android Service的启动过程(上)

    原标题:Android Service的启动过程(上) (点击上方公众号,可快速关注) 来源:伯乐在线专栏作者 - xuyinhuan 链接:http://android.jobbole.com/85 ...

  2. Android Service的启动过程

    刚开始学习Service的时候以为它是一个线程的封装,也可以执行耗时操作.其实不然,Service是运行在主线程的.直接执行耗时操作是会阻塞主线程的.长时间就直接ANR了. 我们知道Service可以 ...

  3. Service 莫名启动解决方案

    最近项目中遇到一个问题:程序在系统注册了broadcastreceiver来监听启动通知,在onReceive中启动了一个service通 过service的onStart来启动程序.问题在于,程序退 ...

  4. android service alertdialog,Android service里面启动alertdialog

    public void showSystemDialog(String showInfo, final int flag) 2 { 3 AlertDialog.Builder b = new Aler ...

  5. android service是什么,Android service是什么 Android service详解

    Android service是什么 Android service详解 时间:2017-04-06     来源:Android开发学习网 什么是Android Service? service是A ...

  6. Android中Service的启动方式及Activity与Service的通信方式

    Service启动的两种方式 1.通过startService启动 使用Service的步骤: 定义一个类继承Service 在Manifest.xml文件中配置该Service 使用Context的 ...

  7. Android WifiDisplay分析一:相关Service的启动

    网址:http://www.2cto.com/kf/201404/290996.html 最近在学习Android 4.4上面的WifiDisplay(Miracast)相关的模块,这里先从WifiD ...

  8. Android Service: 启动service, 停止service

    [ 启动service ] 1. 定义Service类 2. 在manifest中注册 3. 在activity中启动 1. 定义Service类 @Override public IBinder o ...

  9. Android Service是如何启动的?

    为什么80%的码农都做不了架构师?>>>    刚开始学习Android Service的时候以为它是一个线程的封装,也可以执行耗时操作.其实不然,Service是运行在主线程的.直 ...

  10. android服务无法启动,Android服务无法启动(Android service would't start)

    Android服务无法启动(Android service would't start) 我正在尝试在Android中实现简单的服务,但我无法统计基本服务. 这是我的主要课程: import java ...

最新文章

  1. Spring Framework 5.2 正式发布,14项新特性一览
  2. Net EF框架+ MySql示例
  3. 易语言 字段重复_使对易失性字段的操作原子化
  4. html日期默认,html日期控件默认设置为当天日期
  5. Visio 2013专业版
  6. Java加密解密代码小记
  7. multisimbcd码_8421BCD码加法器报告1
  8. MySQL 子查询之 单行子查询及多行子查询
  9. MAC 网速问题 变慢 的来看看 经验
  10. linux宝塔重新安装,Linux服务器Windows系统 安装和卸载宝塔面板
  11. 华为手表开发:WATCH 3 Pro(17)传感器订阅指南针
  12. Unity2019_音效系统
  13. 简单了解latex输出矩阵
  14. java学习(多线程)
  15. 大模型“研究源”告急:2026年高质量语言数据或将耗尽
  16. 小孢子:在?我用本地环境pytest带你玩自定义算子
  17. 数理统计与数据分析第三版习题 第4章
  18. Error:For input string: Consult IDE log for more details (Help | Show Log)
  19. 数据中心配电系统4P开关的应用误区
  20. MapStruct Map 转换成对象

热门文章

  1. SharePoint 2007和WSS 3.0 SDK 1.2 Release
  2. JMJS系统总结系列----XSLT的语句规则(一)
  3. 浅谈JS中的原型对象和原型链
  4. 第二章 算法 (大话数据结构)
  5. 强化学习——从最简单的开始入手
  6. ActiveMQ Stomp的重新投递和死信
  7. makefile函数集锦【转】
  8. 分布式缓存MemcacheHelper
  9. spring实现在一个类中调用另一个类的方法
  10. AOP切面五大通知类型