Android Service 服務不被殺死的妙招
Service是android 系統(tǒng)中的一種組件,它跟Activity的級別差不多,但是他不能自己運行,只能后臺運行,并且可以和其他組件進行交互。
Android開發(fā)的過程中,每次調用startService(Intent)的時候,都會調用該Service對象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些處理。
從Android官方文檔中,我們知道onStartCommand有4種int返回值,首先簡單地講講int返回值的作用。
一、onStartCommand有4種返回值:
START_STICKY:如果service進程被kill掉,保留service的狀態(tài)為開始狀態(tài),但不保留遞送的intent對象。隨后系統(tǒng)會嘗試重新創(chuàng)建service,由于服務狀態(tài)為開始狀態(tài),所以創(chuàng)建服務后一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啟動命令被傳遞到service,那么參數(shù)Intent將為null。
START_NOT_STICKY:“非粘性的”。使用這個返回值時,如果在執(zhí)行完onStartCommand后,服務被異常kill掉,系統(tǒng)不會自動重啟該服務。
START_REDELIVER_INTENT:重傳Intent。使用這個返回值時,如果在執(zhí)行完onStartCommand后,服務被異常kill掉,系統(tǒng)會自動重啟該服務,并將Intent的值傳入。
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務被kill后一定能重啟。
二、創(chuàng)建不被殺死的service
1.在service中重寫下面的方法,這個方法有三個返回值, START_STICKY(或START_STICKY_COMPATIBILITY)是service被kill掉后自動重寫創(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)
{
// 再次動態(tài)注冊廣播
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); // 銷毀時重新啟動Service
this.startService(localIntent);
}
3.創(chuàng)建一個廣播
public class myReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
context.startService(new Intent(context, Google.class));
}
}
4.AndroidManifest.xml中注冊廣播myReceiver及MyService服務
<receiver android:name=".myReceiver" >
<intent-filter android:priority="2147483647" ><!--優(yōu)先級加最高-->
<!-- 系統(tǒng)啟動完成后會調用 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- 解鎖完成后會調用 -->
<action android:name="android.intent.action.USER_PRESENT" />
<!-- 監(jiān)聽情景切換 -->
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
<service android:name=".MyService" >
注:解鎖,啟動,切換場景激活廣播需加權限,如啟動完成,及手機機狀態(tài)等。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
親測ZTE U795手機Android 4.0.4版本adb push到system\app下android:persistent="true"
變成核心程序,在360殺掉進程的時候,myReceiver照樣有效,保證service重生。呃
KILL問題:
1. settings 中stop service
onDestroy方法中,調用startService進行Service的重啟。
2.settings中force stop 應用
捕捉系統(tǒng)進行廣播(action為android.intent.action.PACKAGE_RESTARTED)
3. 借助第三方應用kill掉running task
提升service的優(yōu)先級,程序簽名,或adb push到system\app下等
相較于/data/app下的應用,放在/system/app下的應用享受更多的特權,比如若在其Manifest.xml文件中設置persistent屬性為true,則可使其免受out-of-memory killer的影響。如應用程序'Phone'的AndroidManifest.xml文件:
<application android:name="PhoneApp"
android:persistent="true"
android:label="@string/dialerIconLabel"
android:icon="@drawable/ic_launcher_phone">
...
</application>
設置后app提升為系統(tǒng)核心級別。
- Android系統(tǒng)進程間通信(IPC)機制Binder中的Server和Client獲得Service Manager接口之路
- 淺談Service Manager成為Android進程間通信(IPC)機制Binder守護進程之路
- android開發(fā)教程之開機啟動服務service示例
- Android中實現(xiàn)開機自動啟動服務(service)實例
- Android中Service(后臺服務)詳解
- Android四大組件之Service(服務)實例詳解
- Android創(chuàng)建服務之started service詳細介紹
- Android 通過webservice上傳多張圖片到指定服務器詳解
- Android Service服務詳細介紹及使用總結
- Android 判斷某個服務(service)是否運行
- Android實現(xiàn)在ServiceManager中加入自定義服務的方法詳解
相關文章
Android使用Intent啟動其他非系統(tǒng)應用程序的方法
這篇文章主要介紹了Android使用Intent啟動其他非系統(tǒng)應用程序的方法,實例分析了Intent調用系統(tǒng)應用程序的相關技巧,需要的朋友可以參考下2015-12-12
Android開發(fā)中Activity創(chuàng)建跳轉及傳值的方法
這篇文章主要介紹了Android開發(fā)中Activity創(chuàng)建跳轉及傳值的方法的相關資料,需要的朋友可以參考下2016-05-05
Rxjava+Retrofit+Okhttp進行網(wǎng)絡訪問及數(shù)據(jù)解析
這篇文章主要介紹了Rxjava+Retrofit+Okhttp進行網(wǎng)絡訪問及數(shù)據(jù)解析,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08
Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標簽用法示例
這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標簽用法,實例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下2016-07-07

