码迷,mamicode.com
首页 > 其他好文 > 详细

让自己的服务不被杀掉

时间:2014-06-28 12:22:55      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:android   des   blog   java   使用   strong   

在应用中通常会有需求要让自己的服务不被系统回收,或者回收后立刻再次启动。肯能对系统而言,这样的需求不是太友好,所以实现的方法也很杂。在这里稍稍记录下一些做法。
 
1、通过Notification
通过设置一个不显示的Notification,并标识为前台。
// 设置为前台service
Notification notification = new Notification(); 
startForeground(1, notification);
此时,若要停止服务,可以使用stopForground来实现。
public final void startForeground (int id, Notification notification)

Added in API level 5
Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing. 

  

2、在清单文件里面假冒进程名

 

中可以通过android:process=""来指定进程名,在直接看360等软件杀的时候发现他对某些进程名不感冒。(不知道是不是有意的还是无意的)。
如:你把你的android:process="com.android.acore" 
这种android的核心进程后,在他所列出要杀的名单中就不存在了 。很多手机管家也会不推荐杀死此类进程。

 

3、回收后再次调起

由于回收时系统会调用onDestroy(),这里可操作空间就很大了。随便一想就有三种方式。

4、通过一些系统常见广播来调起服务
比如监听屏幕解锁、电量变化、网络变化等,来调起自己的服务,至少可以保证服务挂了很快能起来。

 

5、通过和其他一些服务的关联来保证自己的服务不被回收
比如一个常用的第三方推送,会有一个常驻进程,并且被各个品牌设备作为白名单,可以通过和他们建立联系,让白名单的进程来实时调起自己的服务

让自己的服务不被杀掉,布布扣,bubuko.com

让自己的服务不被杀掉

标签:android   des   blog   java   使用   strong   

原文地址:http://www.cnblogs.com/LiuSiyuan/p/3798800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!