Talk about the Persistent attribute

1. Background

Add the keyword android:persistent="true" to the AndroidManifest file of the application , and preset the Apk to the system/app directory, so that the application can automatically start and keep alive after booting.

Find the answer from the following two questions:

  1. boot self-start how?
  2. adb kill -9 will automatically restart after killing the process how?

2. Boot self-start process

After booting, check the process number through adb shell ps -A | grep package name, and the process has indeed started.

➜  Desktop psa | grep demo
u0_a61        3329  3329  1753 3600572  83104 ep_poll    7b2c9a4ff34a S e.ecloudapidemo

Check the oom_adj value of the process is -800, the priority is very high, it can be seen that the process with persistent is difficult to be killed by the system.

xxx64:/ # cat proc/3329/oom_score_adj                                                                                                                                    
-800
xxx64:/ # 

The call stack in the startProcessLocked method of ActivityManagerService.java is as follows:

    private boolean startProcessLocked(String hostingType, String hostingNameStr, String entryPoint,
            ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,
            String seInfo, String requiredAbi, String instructionSet, String invokeWith,
            long startTime) {
    
    
        app.pendingStart = true;
        app.killedByAm = false;
        app.removed = false;
        app.killed = false;
        final long startSeq = app.startSeq = ++mProcStartSeqCounter;
        app.setStartParams(uid, hostingType, hostingNameStr, seInfo, startTime);
        if (mConstants.FLAG_PROCESS_START_ASYNC) {
    
    
            if (DEBUG_PROCESSES) Slog.i(TAG_PROCESSES,
                    "Posting procStart msg for " + app.toShortString());
            
        	//Add by QXL
        	if(null != app && app.processName != null && app.processName.equals("com.example.ecloudapidemo")) {
    
    
        		android.util.Log.d("qxl","ams startProcessLocked stack:"
        					+ android.util.Log.getStackTraceString(new Throwable()));
        	}

重启设备抓开机Log,结果如下:
02-07 01:09:41.247 1941 1956 D qxl : ams startProcessLocked stack:java.lang.Throwable
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4486)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4451)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.ActivityManagerService.addAppLocked(ActivityManagerService.java:13289)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.ActivityManagerService.addAppLocked(ActivityManagerService.java:13253)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.ActivityManagerService.startPersistentApps(ActivityManagerService.java:13011)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.UserController$Injector.startPersistentApps(UserController.java:2195)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.UserController.finishUserUnlocked(UserController.java:449)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.am.UserController.handleMessage(UserController.java:1999)
02-07 01:09:41.247 1941 1956 D qxl : at android.os.Handler.dispatchMessage(Handler.java:102)
02-07 01:09:41.247 1941 1956 D qxl : at android.os.Looper.loop(Looper.java:193)
02-07 01:09:41.247 1941 1956 D qxl : at android.os.HandlerThread.run(HandlerThread.java:65)
02-07 01:09:41.247 1941 1956 D qxl : at com.android.server.ServiceThread.run(ServiceThread.java:44)

The process of booting and self-starting is relatively clear.

3. adb kill -9 will automatically restart after killing the process

Although the application process oom_adj with the persistent feature is -800, it is difficult to be killed by the system, but other factors may cause the application process to be killed. For example, we can kill a process by commanding adb shell kill -9 xxx.

Try it on the above persistent application:

adb shell kill -9 3329

此时打印调用栈信息如下:
02-07 01:10:37.374 1941 2066 D qxl : ams startProcessLocked stack:java.lang.Throwable
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4486)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4451)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4266)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:4260)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.cleanUpApplicationRecordLocked(ActivityManagerService.java:20377)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:5968)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:6162)
02-07 01:10:37.374 1941 2066 D qxl : at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1897)
02-07 01:10:37.374 1941 2066 D qxl : at android.os.BinderProxy.sendDeathNotice(Binder.java:1193)

It can be seen that after the persistent process is killed, the binder tombstone mechanism between the process startup and AMS is triggered to the cleanUpApplicationRecordLocked method, and the AMS for the persistent application will restart a new process.

How to bind such a relationship with AMS after the application process starts? You can refer to the flowchart drawn by others:
insert image description here

4. Summary

After roughly figuring out the implementation of persistent, we can customize the keep-alive and self-starting lists based on this mechanism. On the Application side, you can rewrite the onCreate method of the Application to do the business logic after the process is started.

Guess you like

Origin blog.csdn.net/lgglkk/article/details/128918614