让Android应用不被杀死(killer)的方法
方法:
对于一个service,可以首先把它设为在前台运行:
publicvoidMyService.onCreate(){
super.onCreate();
Notificationnotification=newNotification(android.R.drawable.my_service_icon,
"my_service_name",
System.currentTimeMillis());
PendingIntentp_intent=PendingIntent.getActivity(this,0,
newIntent(this,MyMainActivity.class),0);
notification.setLatestEventInfo(this,"MyServiceNotification,"MyServiceNotificationisRunning!", p_intent);
Log.d(TAG,String.format("notification=%s",notification));
startForeground(0x1982,notification); //notificationID:0x1982,youcannameitasyouwill.
}
-------------------------------
相较于/data/app下的应用,放在/system/app下的应用享受更多的特权,比如若在其Manifest.xml文件中设置persistent属性为true,则可使其免受out-of-memorykiller的影响。如应用程序'Phone'的AndroidManifest.xml文件:
<applicationandroid:name="PhoneApp"
android:persistent="true"
android:label="@string/dialerIconLabel"
android:icon="@drawable/ic_launcher_phone">
...
</application>
设置后app提升为系统核心级别,任何情况下不会被kill掉,settings->applications里面也会屏蔽掉stop操作。
这样设置前的log:
Proc#19:adj=svc /B4067b028255:com.xxx.xxx/10001(started-services) #cat/proc/255/oom_adj 4
设置后的log:
PERS#19:adj=core/F406291f0155:com.xxx.xxx/10001(fixed)
#cat/proc/155/oom_adj -12 #这是CORE_SERVER_ADJ