1.在service中重寫下面的方法,這個方法有三個傳回值, START_STICKY是service被kill掉後自動重寫建立
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}----------------
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("TrafficService","startCommand");
flags = START_STICKY;
return super.onStartCommand(intent, flags, startId);
// return START_REDELIVER_INTENT;
}
2.在Service的onDestroy()中重啟Service.
public void onDestroy() {
Intent localIntent = new Intent();
localIntent.setClass(this, MyService.class); //銷毀時重新啟動Service
this.startService(localIntent);
}---------------------------------------------
用qq管家殺掉進程的時候,調用的是系統內建的強制kill功能(即settings裡的),在kill時,會將應用的整個進程停掉,當然包括service在內,如果在running裡將service強制kill掉,顯示進程還在。不管是kill整個進程還是只kill掉進應用的 service,都不會重新啟動service。不知道你是怎麼怎麼實現重啟的,實在是不解。。。
ps:在eclipse中,用stop按鈕kill掉進程的時候,倒是會重啟service
KILL問題:
1. settings 中stop service
onDestroy方法中,調用startService進行Service的重啟。
2.settings中force stop 應用
捕捉系統進行廣播(action為android.intent.action.PACKAGE_RESTARTED)
3. 藉助第三方應用kill掉running task
提升service的優先順序