Android下写一个永远不会被KILL掉进程-服务 下载本文

内容发布更新时间 : 2024/6/7 22:01:38星期一 下面是文章的全部内容请认真阅读。

★精品文档★

通过dumpsys activity命令能够很明显的看出其中差别。

代码如下:

Running processes (most recent first):

App # 3: adj= 2/1 ProcessRecord{30858c20 1877:com.android.email/10014} (started-services) PERS # 2: adj=-100/0 ProcessRecord{308fb390 1713:system/1000} (fixed)

App # 1: adj= 0/0 ProcessRecord{30908198 1794:android.process.acore/10005} (top-activity) PERS # 0: adj= -12/0 ProcessRecord{3090d488 1789:xiao.xiong.test/10026} (fixed) 代码如下:

而且adj=-12时,这个进程通过ddms手动stop后会立即启动

我是天王盖地虎的分割线 方法

对于一个service,可以首先把它设为在前台运行:

代码如下:

2016全新精品资料-全新公文范文-全程指导写作 –独家原创

6 / 18

★精品文档★

public void MyService.onCreate() { super.onCreate(); Notification

notification

=

new

Notification(android.R.drawable.my_service_icon,”my_service_name”,System.currentTimeMillis()); PendingIntent

p_intent

=

PendingIntent.getActivity(this, 0,

new Intent(this, MyMainActivity.class), 0); notification.setLatestEventInfo(this,

“MyServiceNotification, “MyServiceNotification is Running!”,p_intent);

Log.d(TAG, String.format(“notification = %s”, notification));

startForeground(0x1982,

notification);

//

notification ID: 0x1982, you can name it as you will. }

代码如下:

相较于/data/app下的应用,放在/system/app下的应用享受更多的特权,比如若在其Manifest.xml文件中设置persistent属性为true,则可使其免受out-of-memory killer的影响。

2016全新精品资料-全新公文范文-全程指导写作 –独家原创

7 / 18

★精品文档★

如应用程序’Phone’的AndroidManifest.xml文件:

<application android: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 /B 4067b028 255:com.xxx.xxx/10001 (started-services) # cat /proc/255/oom_adj 4

设置后的log:

PERS #19: adj=core /F 406291f0 155:com.xxx.xxx/10001 (fixed)

# cat /proc/155/oom_adj -12 # 这是CORE_SERVER_ADJ

2016全新精品资料-全新公文范文-全程指导写作 –独家原创

8 / 18

★精品文档★

注:init进程的oom_adj为-16(即SYSTEM_ADJ): cat /proc/1/oom_adj 代码如下:

Android相关部分分析 在

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java中有以下的代码:

代码如下:

final ProcessRecord addAppLocked(ApplicationInfo info) {

ProcessRecord

app

=

getProcessRecordLocked(info.processName, info.uid);

if (app == null) {

app = newProcessRecordLocked(null, info, null); mProcessNames.put(info.processName, app);

updateLruProcessLocked(app, true, true); } if

info.uid,

2016全新精品资料-全新公文范文-全程指导写作 –独家原创

9 / 18

★精品文档★

((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) ==

(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {

app.persistent = true;

app.maxAdj = CORE_SERVER_ADJ; // 这个常数值为-12。 } if

(app.thread

==

null

&&

mPersistentStartingProcesses.indexOf(app) < 0) { mPersistentStartingProcesses.add(app);

startProcessLocked(app, “added application”, app.processName); }

return app; }

代码如下:

可见要想成为core service (即app.maxAdj = CORE_SERVER_ADJ(-12)),应用程序需要FLAG_SYSTEM和FLAG_PERSISTENT两个标志,FLAG_SYSTEM指的是应用位于/system/app下,FLAG_PERSISTENT就是指persistent属性。

2016全新精品资料-全新公文范文-全程指导写作 –独家原创

10 / 18