Service防止被Kill的方法整理

2021-06-27 23:25:49 字數 3075 閱讀 4542

在開發過程中,常常需要開啟一些後台服務,來處理一些資料操作,即使使用者退出程式後,後台也能夠正常的處理資料,此時就需要使用到service,關於service的使用,在這裡就不做詳細介紹了,今天討論的是,即使我們使用service,也並不一定能保證程式的正常執行,為什麼呢?因為市面上存在很多清理程序的軟體,比較常見的就是獵豹、360等,而且絕大多數的使用者也都會安裝這類軟體以清理手機記憶體。當使用者使用此類軟體清理程序後,絕大多數的service都被kill掉,如果你的service非常重要,此時被kill就會導致程式的不正常執行,那麼如何保持service的常駐記憶體而不被kill掉呢?下面我整理了如下幾個方法來防止service被kill

方法一:提高service的優先順序

對於service被系統**,一般做法是通過提高優先順序可以解決,在androidmanifest.xml檔案中對於intent-filter可以通過android:priority = "1000"這個屬性設定最高優先順序,1000是最高值,如果數字越小則優先順序越低,同時實用於廣播,推薦大家如果你的應用很重要,可以考慮通過系統常用intent action來觸發。 具體**如下

<

service

android:name

="com.jarvis.widget.openserviceforwidget"

android:enabled

="true"

android:priority

="1000"

>

<

intent

-filter

>

<

action

android:name

="openserviceforwidget"/>

-filter

>

>

方法二:保持service前台執行

android 系統對於記憶體管理有自己的一套方法,為了保障系統有序穩定的運信,系統內部會自動分配,控制程式的記憶體使用。當系統覺得當前的資源非常有限的時候,為了保 證一些優先順序高的程式能執行,就會殺掉一些他認為不重要的程式或者服務來釋放記憶體。這樣就能保證真正對使用者有用的程式仍然再執行。如果你的 service 碰上了這種情況,多半會先被殺掉。但如果你增加 service 的優先順序就能讓他多留一會,我們可以用 setforeground(true) 來設定 service 的優先順序。

在乙個開啟的service中的oncreat方法中設定,前台執行(此方法不會在通知欄顯示前台執行的service資訊): 

notification

notification

=new

notification();

startforeground(1, notification);

當然,如果你需要讓使用者知道你的這個service正在執行,可以在通知欄中顯示乙個標籤,告訴使用者此service正在執行:

notification

notification

=new

notification(r.drawable.ic_launcher, "服務開

啟", system.currenttimemillis());

notification.flags|=

notification.flag_no_clear;  

notification.flags

=notification.flag_ongoing_event;

intent

notificationintent

=new

intent(this, mainactivity.class);

pendingintent

pendingintent

=pendingintent.getactivity(this, 0, notificationintent, 0);

notification.setlatesteventinfo(this, "service", "防止服

務被任務

管理器所

殺", pendingintent);     

startforeground(1 , notification);

如果此時你又想使service在後台執行,我們可以呼叫這個方法來設定:

//取消通

知欄標籤(最好在

設定後台

執行前取

消,因為

我們有可

能在取消

前台服務

之後的那

一瞬間被kill掉。

//這個時

候 notification 便永遠

不會從通

知一欄移

除)

notificationmanager

notificationmanager

=(notificationmanager) getsystemservice(context.notification_service);

notificationmanager.cancel(1);

//使service進入後

臺執行

stopforeground(false);

方法三:

在service中重寫下面的方法,這個方法有三個返回值, start_sticky是service被kill掉後自動重寫建立

@override

public

intonstartcommand(intent

intent, int

flags, int

startid) 

在service的ondestroy()中重啟service.

public

void

ondestroy() 

方法四:不可濫用的方法,此方法的使用需慎重

//在androidmanifest.xml中配置

此屬性android:persistent="true"   

<

android:persistent

="true"

>

>

目前系統中有phone等非常有限的,必須一直活著的應用在使用。

參考:pendingintent的理解

保證service在後台不被kill

android 其實無法做多絕對的不被後台kill掉,我們只能盡量使用一些操作提公升不被kill的機會。一 onstartcommand方法,返回start sticky start sticky 在執行onstartcommand後service程序被kill後,那將保留在開始狀態,但是不保留那些...

如何保證service不被kill掉的方法總結

kill的情況有幾種 1.在設定的執行裡kill掉service,這種情況可以ondestroy方法中,呼叫startservice進行service的重啟。2.在設定裡面強制停止捕捉系統進行廣播 action為android.intent.action.package restarted 3.借助...

如何保證service在後台不被kill

推薦參考部落格 一 onstartcommand方法,返回start sticky 1 start sticky 在執行onstartcommand後service程序被kill後,那將保留在開始狀態,但是不保留那些傳入的intent。不久後service就會再次嘗試重新建立,因為保留在開始狀態,在...