前台服务+杀不死的方案

(1)在主activity处设置属性:android:excludeFromRecents=“true” 属性的作用恰恰就是让你在长按home键的时候在弹出的应用列表中隐藏你的应用,达到隐藏应用程序进行的目的。
(2)如果某个进程不想被杀死(如数据缓存进程,或状态监控进程,或远程服务进程),应该怎么做,才能使进程不被杀死。
add android:persistent="true" into the section in your AndroidManifest.xml
切记,这个不可滥用,系统中用这个的service,app一多,整个系统就完蛋了。
目前系统中有phone等非常有限的,必须一直活着的应用在试用。
(3)提升service优先级的方法
可以用 setForeground(true) 来设置 Service 的优先级
(4)对于Service被系统回收,一般做法是通过提高优先级可以解决,在AndroidManifest.xml文件中对于intent-filter可以通过android:priority = "1000"这个属性设置最高优先级,1000是最高值,如果数字越小则优先级越低,同时实用于广播,推荐大家如果你的应用很重要,可以考虑通过系统常用intent action来触发。
(4)线程优先级(Thread-Priority),我们需要在AndroidManifest.xml 中使用 'uses-permission' 这样做:
XML:

可以在你的Activity中使用以下代码改变或提高任何线程的优先级:

// Changes the Priority of the calling Thread!
Process.setThreadPriority(12);
// Changes the Priority of passed Thread (first param)
Process.setThreadPriority(Process.myTid(), 12);

这里 range 的范围是 -20 (高) 到 +19 (低). 不要选得 太高  

最好使用预先定义在 android.os.Process 的constants :
Java: 
          // Lower is 'more impotant'
Process.THREAD_PRIORITY_LOWEST = 19
Process.THREAD_PRIORITY_BACKGROUND = 5
Process.THREAD_PRIORITY_DEFAULT = 0
Process.THREAD_PRIORITY_FOREGROUND = -5
Process.THREAD_PRIORITY_DISPLAY = -10
Process.THREAD_PRIORITY_URGENT_DISPLAY = -15


你可能感兴趣的:(前台服务+杀不死的方案)