Scheduling restart of crashed service 如何提升Service的优先级避免被杀死或者杀死后如何再次重启Service?

    08-16 11:38:23.000: W/ActivityManager(389): Scheduling restart of crashed servicehtml

1. ActivityManager 与 service之间的关系android

2. service正常关闭与异常关闭的状况app

 

【http://www.cnblogs.com/wlrhnh/p/3529926.html】post

如何提升Service的优先级避免被杀死或者杀死后如何再次重启Service?this

咱们知道,当进程长期不活动时,若是系统资源吃紧,会杀死一些Service,或不可见的Activity等所在的进程。 url

如何避免Service被系统杀死,随便在网上搜一下,都能搜到好几种方法,可是每一种方法都有不一样的适用环境。spa

1. 添加android:persistent="true"rest

添加android:persistent="true"到AndroidManifest.xml,Google文档描述以下:code

Whether or not the application should remain running at all times-true" if it should, and "false"if not. The default value is "false". Applications should not normally set this flag; persistence mode is intended only for certain system applications.可见这个属性不能随便用,到目前为止,我所发现使用该属性的应用只有Phone,并且使用是要求权限的,因此这个属性对第三方应用来讲意义不是很大;orm

2. 设置onStartCommand()的返回值

这个思路比较有用,咱们着重分析一下,该方法有四种返回值:

 

TART_STICKY:kill后会被重启,可是重启后调用onStarfCommand()传进来的Intent参数为null,说明被kill的时候没有保存Intent;

START_NOT_STICKY:kill以后不会被重启;

相关文章
相关标签/搜索