Android Service 服務不被殺死的妙招_Android

來源:互聯網
上載者:User

Service是android 系統中的一種組件,它跟Activity的層級差不多,但是他不能自己運行,只能後台運行,並且可以和其他組件進行互動。

Android開發的過程中,每次調用startService(Intent)的時候,都會調用該Service對象的onStartCommand(Intent,int,int)方法,然後在onStartCommand方法中做一些處理。
從Android官方文檔中,我們知道onStartCommand有4種int傳回值,首先簡單地講講int傳回值的作用。

一、onStartCommand有4種傳回值:

START_STICKY:如果service進程被kill掉,保留service的狀態為開始狀態,但不保留遞送的intent對象。隨後系統會嘗試重新建立service,由於服務狀態為開始狀態,所以建立服務後一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啟動命令被傳遞到service,那麼參數Intent將為null。
START_NOT_STICKY:“非粘性的”。使用這個傳回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啟該服務。

START_REDELIVER_INTENT:重傳Intent。使用這個傳回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啟該服務,並將Intent的值傳入。

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

二、建立不被殺死的service

1.在service中重寫下面的方法,這個方法有三個傳回值, START_STICKY(或START_STICKY_COMPATIBILITY)是service被kill掉後自動重寫建立

@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; }@Overridepublic 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" />

 親測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 應用

捕捉系統進行廣播(action為android.intent.action.PACKAGE_RESTARTED)

3. 藉助第三方應用kill掉running task

提升service的優先順序,程式簽名,或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提升為系統核心層級。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.