Android Service 服務(wù)不被殺死的妙招
Service是android 系統(tǒng)中的一種組件,它跟Activity的級(jí)別差不多,但是他不能自己運(yùn)行,只能后臺(tái)運(yùn)行,并且可以和其他組件進(jìn)行交互。
Android開(kāi)發(fā)的過(guò)程中,每次調(diào)用startService(Intent)的時(shí)候,都會(huì)調(diào)用該Service對(duì)象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些處理。
從Android官方文檔中,我們知道onStartCommand有4種int返回值,首先簡(jiǎn)單地講講int返回值的作用。
一、onStartCommand有4種返回值:
START_STICKY:如果service進(jìn)程被kill掉,保留service的狀態(tài)為開(kāi)始狀態(tài),但不保留遞送的intent對(duì)象。隨后系統(tǒng)會(huì)嘗試重新創(chuàng)建service,由于服務(wù)狀態(tài)為開(kāi)始狀態(tài),所以創(chuàng)建服務(wù)后一定會(huì)調(diào)用onStartCommand(Intent,int,int)方法。如果在此期間沒(méi)有任何啟動(dòng)命令被傳遞到service,那么參數(shù)Intent將為null。
START_NOT_STICKY:“非粘性的”。使用這個(gè)返回值時(shí),如果在執(zhí)行完onStartCommand后,服務(wù)被異常kill掉,系統(tǒng)不會(huì)自動(dòng)重啟該服務(wù)。
START_REDELIVER_INTENT:重傳Intent。使用這個(gè)返回值時(shí),如果在執(zhí)行完onStartCommand后,服務(wù)被異常kill掉,系統(tǒng)會(huì)自動(dòng)重啟該服務(wù),并將Intent的值傳入。
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務(wù)被kill后一定能重啟。
二、創(chuàng)建不被殺死的service
1.在service中重寫(xiě)下面的方法,這個(gè)方法有三個(gè)返回值, START_STICKY(或START_STICKY_COMPATIBILITY)是service被kill掉后自動(dòng)重寫(xiě)創(chuàng)建
@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY_COMPATIBILITY; //return super.onStartCommand(intent, flags, startId); }
或
@Override public int onStartCommand(Intent intent, int flags, int startId) { flags = START_STICKY; return super.onStartCommand(intent, flags, startId); // return START_REDELIVER_INTENT; } @Override public void onStart(Intent intent, int startId) { // 再次動(dòng)態(tài)注冊(cè)廣播 IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT"); localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值 myReceiver searchReceiver = new myReceiver(); registerReceiver(searchReceiver, localIntentFilter); super.onStart(intent, startId); }
2.在Service的onDestroy()中重啟Service.
public void onDestroy() { Intent localIntent = new Intent(); localIntent.setClass(this, MyService.class); // 銷(xiāo)毀時(shí)重新啟動(dòng)Service this.startService(localIntent); }
3.創(chuàng)建一個(gè)廣播
public class myReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, Google.class)); } }
4.AndroidManifest.xml中注冊(cè)廣播myReceiver及MyService服務(wù)
<receiver android:name=".myReceiver" > <intent-filter android:priority="2147483647" ><!--優(yōu)先級(jí)加最高--> <!-- 系統(tǒng)啟動(dòng)完成后會(huì)調(diào)用 --> <action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- 解鎖完成后會(huì)調(diào)用 --> <action android:name="android.intent.action.USER_PRESENT" /> <!-- 監(jiān)聽(tīng)情景切換 --> <action android:name="android.media.RINGER_MODE_CHANGED" /> </intent-filter> </receiver> <service android:name=".MyService" >
注:解鎖,啟動(dòng),切換場(chǎng)景激活廣播需加權(quán)限,如啟動(dòng)完成,及手機(jī)機(jī)狀態(tài)等。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
親測(cè)ZTE U795手機(jī)Android 4.0.4版本adb push到system\app下android:persistent="true"
變成核心程序,在360殺掉進(jìn)程的時(shí)候,myReceiver照樣有效,保證service重生。呃
KILL問(wèn)題:
1. settings 中stop service
onDestroy方法中,調(diào)用startService進(jìn)行Service的重啟。
2.settings中force stop 應(yīng)用
捕捉系統(tǒng)進(jìn)行廣播(action為android.intent.action.PACKAGE_RESTARTED)
3. 借助第三方應(yīng)用kill掉running task
提升service的優(yōu)先級(jí),程序簽名,或adb push到system\app下等
相較于/data/app下的應(yīng)用,放在/system/app下的應(yīng)用享受更多的特權(quán),比如若在其Manifest.xml文件中設(shè)置persistent屬性為true,則可使其免受out-of-memory killer的影響。如應(yīng)用程序'Phone'的AndroidManifest.xml文件:
<application android:name="PhoneApp" android:persistent="true" android:label="@string/dialerIconLabel" android:icon="@drawable/ic_launcher_phone"> ... </application>
設(shè)置后app提升為系統(tǒng)核心級(jí)別。
- Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接口之路
- 淺談Service Manager成為Android進(jìn)程間通信(IPC)機(jī)制Binder守護(hù)進(jìn)程之路
- android開(kāi)發(fā)教程之開(kāi)機(jī)啟動(dòng)服務(wù)service示例
- Android中實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
- Android中Service(后臺(tái)服務(wù))詳解
- Android四大組件之Service(服務(wù))實(shí)例詳解
- Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
- Android 通過(guò)webservice上傳多張圖片到指定服務(wù)器詳解
- Android Service服務(wù)詳細(xì)介紹及使用總結(jié)
- Android 判斷某個(gè)服務(wù)(service)是否運(yùn)行
- Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法詳解
相關(guān)文章
Android實(shí)現(xiàn)View滑動(dòng)效果的6種方法
這篇文章主要介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android滾動(dòng)菜單ListView實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Android滾動(dòng)菜單ListView實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
只有你允許客戶端從不同的應(yīng)用程序?yàn)榱诉M(jìn)程間的通信而去訪問(wèn)你的service,以及想在你的service處理多線程,下面為大家詳細(xì)介紹下2013-06-06Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
Android利用方向傳感器實(shí)現(xiàn)指南針功能,聽(tīng)起來(lái)還不錯(cuò)吧,下面與大家分享下具體的實(shí)現(xiàn)步驟,感興趣的朋友可以參考下哈2013-06-06Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
桌面快捷方式的出現(xiàn)方便了用戶操作,在某些程度上提高了用戶體驗(yàn),接下來(lái)將介紹下Android創(chuàng)建/驗(yàn)證/刪除桌面快捷方式的實(shí)現(xiàn)思路及代碼,感興趣的朋友可以了解下,或許本文可以幫助到你2013-02-02Android獲取網(wǎng)絡(luò)圖片并顯示的方法
這篇文章主要為大家詳細(xì)介紹了Android獲取網(wǎng)絡(luò)圖片并顯示的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android自定義ViewGroup實(shí)現(xiàn)九宮格布局
這篇文章主要為大家詳細(xì)介紹了Android如何通過(guò)自定義ViewGroup實(shí)現(xiàn)九宮格布局,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12