android如何讓service不被殺死-提升進程優先級



1.在service中重寫下面的方法,這個方法有三個返回值, START_STICKY是service被kill掉後自動重寫建立
[代碼]java代碼:
@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。不知道你是怎麼實現重啓的,實在是不解。 在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開機啓動
今天咱們主要來探討android怎麼讓一個service開機自動啓動功能的實現。Android手機在啓動的過程當中會觸發一個Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED(記得只會觸發一次呀),在這裏咱們能夠經過構建一個廣播接收者來接收這個這個action.下面我就來簡單寫如下實現的步驟: 
第一步:首先建立一個廣播接收者,重構其抽象方法 onReceive(Context context, Intent intent),在其中啓動你想要啓動的Service或app。
[代碼]java代碼:
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文件,在re
ceiver接收這種添加intent-filter配置 
[代碼]java代碼:
<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來觸發。




下面這是從另外的網頁上看到的,一併轉過來了:http://goo.gl/eEfBup java

爲了提升 咱們的Activity中的線程的
線程優先級(Thread-Priority),咱們須要在AndroidManifest.xml 中使用 'uses-permission' 這樣作:
XML: 
          <uses-permission id="android.permission.RAISED_THREAD_PRIORITY"/>

  如今你能夠在你的Activity中使用如下代碼改變或提升任何線程的優先級:
Java: 
          import android.os.Process;
// ...

// -----------------------------------
// Set the priority of the calling thread, based on Linux priorities:
// -----------------------------------

// Changes the Priority of the calling Thread!
Process.setThreadPriority(12);
// Changes the Priority of passed Thread (first param)
Process.setThreadPriority(Process.myTid(), 12);

  這裏 range 的範圍是 -20 (高) 到 +19 (低). 不要選得 過高  

  最好使用預先定義在 android.os.Process 的constants :
Java: 
          // Lower is 'more impotant'
Process.THREAD_PRIORITY_LOWEST = 19
Process.THREAD_PRIORITY_BACKGROUND = 5
Process.THREAD_PRIORITY_DEFAULT = 0
Process.THREAD_PRIORITY_FOREGROUND = -5
Process.THREAD_PRIORITY_DISPLAY = -10
Process.THREAD_PRIORITY_URGENT_DISPLAY = -15
android

相關文章
相關標籤/搜索