Android開發的過程當中,每次調用startService(Intent)的時候,都會調用該Service對象的onStartCommand(Intent,int,int)方法,而後在onStartCommand方法中作一些處理。android
從Android官方文檔中,咱們知道onStartCommand有4種int返回值,首先簡單地講講int返回值的做用。app
1、onStartCommand有4種返回值:ide
START_STICKY:若是service進程被kill掉,保留service的狀態爲開始狀態,但不保留遞送的intent對象。隨後系統會嘗試從新建立service,因爲服務狀態爲開始狀態,因此建立服務後必定會調用onStartCommand(Intent,int,int)方法。若是在此期間沒有任何啓動命令被傳遞到service,那麼參數Intent將爲null。this
START_NOT_STICKY:「非粘性的」。使用這個返回值時,若是在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啓該服務。xml
START_REDELIVER_INTENT:重傳Intent。使用這個返回值時,若是在執行完onStartCommand後,服務被異常kill掉,系統會自動重啓該服務,並將Intent的值傳入。對象
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務被kill後必定能重啓。進程
2、建立不被殺死的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);
}string
或
@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" />
親測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提高爲系統核心級別