Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Service 不被殺死的幾種常用技巧

編輯:關於 Android 程式設計

Service 是 Android 系統中的一種組件,級別與 Activity 相當,但它不能自行運行,只能後台運行,並可與其他組件交互。本文總結了幾種提升 Service 存活能力的常用技巧,供大家參考。

每次調用 startService(Intent) 時都會觸發該 Service 的 onStartCommand(Intent, int, int) 方法。從官方文檔可知,onStartCommand 有 4 種 int 返回值。

一、onStartCommand 的四種返回值

START_STICKY:若 service 進程被 kill,系統會保留其為「已啟動」狀態並嘗試重建,重建後一定會調用 onStartCommand;若期間沒有新的啟動命令,Intent 參數為 null。

START_NOT_STICKY:「非粘性」,執行完 onStartCommand 後若服務被異常 kill,系統不會自動重啟。

START_REDELIVER_INTENT:若服務被異常 kill,系統會自動重啟並把最後的 Intent 重新傳入。

START_STICKY_COMPATIBILITY:START_STICKY 的兼容版本,但不保證服務被 kill 後一定能重啟。

二、創建更強存活能力的 Service

1、重寫 onStartCommand 返回 START_STICKY 或 START_STICKY_COMPATIBILITY:

 @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) { // 再次動態注冊廣播 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、創建一個廣播接收器:

 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" ><!--優先級加最高--> <!-- 系統啟動完成後會調用 --> <action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- 解鎖完成後會調用 --> <action android:name="android.intent.action.USER_PRESENT" /> <!-- 監聽情景切換 --> <action android:name="android.media.RINGER_MODE_CHANGED" /> </intent-filter> </receiver> <service android:name=".MyService" >

注意:解鎖、啟動、切換場景激活廣播需相應權限,如:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />

三、針對不同 KILL 場景的應對

1、settings 中 stop service:在 onDestroy 中調用 startService 重啟 Service。

2、settings 中 force stop 應用:捕捉系統廣播(action 為 android.intent.action.PACKAGE_RESTARTED)。

3、借助第三方應用 kill 掉 running task:可提升 Service 優先級、進行程序簽名,或 adb push 到 system\app 下等。

相較於 /data/app 下的應用,放在 /system/app 下的應用享受更多特權。例如在其 Manifest 中設置 android:persistent="true",可使其免受 out-of-memory killer 的影響:

 <application android:name="PhoneApp" android:persistent="true" android:label="@string/dialerIconLabel" android:icon="@drawable/ic_launcher_phone"> ... </application>

需要客觀說明的是,隨著 Android 版本的不斷升級,系統對後台進程的限制日益嚴格,上述部分技巧在新系統上可能失效,且頻繁喚醒後台服務也會增加電量消耗。合理的做法是採用官方推薦的 WorkManager、前台服務(Foreground Service)等機制來規劃後台任務。以上就是提升 Service 存活能力的常見思路,希望對大家的開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved