android如何讓service不被殺死

來源:互聯網
上載者:User

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的優先順序
--------------------------------------------
service開機啟動

http://blog.csdn.net/flying_vip_521/article/details/7053355

  • 今天我們主要來探討android怎麼讓一個service開機自動啟動功能的實現。Android手機在啟動的過程中會觸發一個Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED(記得只會觸發一次呀),在這裡我們可以通過構建一個廣播接收者來接收這個這個action.下面我就來簡單寫以下實現的步驟:
  • 第一步:首先建立一個廣播接收者,重構其抽象方法 onReceive(Context context, Intent intent),在其中啟動你想要啟動的Service或app。
    • import android.content.BroadcastReceiver;
    • import android.content.Context;
    • import android.content.Intent;
    • import android.util.Log;
    • public class BootBroadcastReceiver extends BroadcastReceiver {
    • //重寫onReceive方法
    • @Override
    • public void onReceive(Context context, Intent intent) {
    • //後邊的XXX.class就是要啟動的服務
    • Intent service = new Intent(context,XXXclass);
    • context.startService(service);
    • Log.v("TAG", "開機自動服務自動啟動.....");
    • //啟動應用,參數為需要自動啟動的應用的包名
    • Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    • context.startActivity(intent );
    • }
    • }
  • 第二步:配置xml檔案,在receiver接收這種添加intent-filter配置
  • <receiver android:name="BootBroadcastReceiver">
  • <intent-filter>
  • <action android:name="android.intent.action.BOOT_COMPLETED"></action>
  • <category android:name="android.intent.category.LAUNCHER" />
  • </intent-filter>
  • </receiver>
  • 第三步:添加許可權 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


---------------------------------------------------
如何?一個不會被殺死的進程
看Android的文檔知道,當進程長期不活動,或系統需要資源時,會自動清理門戶,殺死一些Service,和不可見的Activity等所在的進程。
但是如果某個進程不想被殺死(如資料緩衝進程,或狀態監控進程,或遠程服務進程),應該怎麼做,才能使進程不被殺死。

add android:persistent="true" into the <application> section in your AndroidManifest.xml

切記,這個不可濫用,系統中用這個的service,app一多,整個系統就完蛋了。
目前系統中有phone等非常有限的,必須一直活著的應用在試用。

------------------------------------------------
提升service優先順序的方法

Android 系統對於記憶體管理有自己的一套方法,為了保障系統有序穩定的運信,系統內部會自動分配,控製程序的記憶體使用量。當系統覺得當前的資源非常有限的時候,為了保 證一些優先順序高的程式能運行,就會殺掉一些他認為不重要的程式或者服務來釋放記憶體。這樣就能保證真正對使用者有用的程式仍然再運行。如果你的 Service 碰上了這種情況,多半會先被殺掉。但如果你增加 Service 的優先順序就能讓他多留一會,我們可以用 setForeground(true) 來設定 Service 的優先順序。

  為什麼是 foreground ? 預設啟動的 Service 是被標記為 background,當前啟動並執行 Activity 一般被標記為 foreground,也就是說你給 Service 設定了 foreground 那麼他就和正在啟動並執行 Activity 類似優先順序得到了一定的提高。當讓這並不能保證你得 Service 永遠不被殺掉,只是提高了他的優先順序。

  從Android 1.5開始,一個已啟動的service可以調用startForeground(int, Notification)將service置為foreground狀態,調用stopForeground(boolean)將service置為 background狀態。

  我們會在調用startForeground(int, Notification)傳入參數notification,它會在狀態列裡顯示進行中的foreground service。background service不會在狀態列裡顯示。

  在Android 1.0中,將一個service置為foreground狀態:
  setForeground(true);
  mNM.notify(id, notification);
  將一個service置為background狀態:
  mNM.cancel(id);
  setForeground(false);
  對比看出,在1.0 API中調用setForeground(boolean)只是簡單的改變service的狀態,使用者不會有任何覺察。新API中強制將 notification和改變service狀態的動作綁定起來,foreground service會在狀態列顯示,而background service不會。

  Remote service controller & binding
  跨進程調用Service。暫時不研究。
-------------------------------------------------------
如何防止Android應用中的Service被系統回收? 很多朋友都在問,如何防止Android應用中的Service被系統回收?下面簡單解答一下。

對於Service被系統回收,一般做法是通過提高優先順序可以解決,在AndroidManifest.xml檔案中對於intent-filter可以通過android:priority = "1000"這個屬性設定最高優先順序,1000是最高值,如果數字越小則優先順序越低,同時實用於廣播,推薦大家如果你的應用很重要,可以考慮通過系統常用intent action來觸發。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.