前臺服務+殺不死的方案

(1)在主activity處設置屬性:android:excludeFromRecents=“true” 屬性的作用恰恰就是讓你在長按home鍵的時候在彈出的應用列表中隱藏你的應用,達到隱藏應用程序進行的目的。
(2)如果某個進程不想被殺死(如數據緩存進程,或狀態監控進程,或遠程服務進程),應該怎麼做,才能使進程不被殺死。
add android:persistent="true" into the <application> 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: <uses-permission id="android.permission.RAISED_THREAD_PRIORITY"/>

可以在你的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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章