让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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下