Resources:
Http://www.th7.cn/Program/Android/201406/213377.shtml
By adding the android:persistent= "true" attribute to the application tag in Androidmanifest.xml, it is true that the process is guaranteed not to be killed by LMK. However, the premise is that the application must be a system application, that is, the application cannot use the usual installation method. The app's APK package must be placed directly under the/system/app directory. And the system must be restarted before it can take effect .
===============================
1. Android:persistent
There is a permanent application in the Android system. In their corresponding androidmanifest.xml file, the persistent property is set to true.
At the time of system startup, the Systemready () of AMS will load all applications that are persistent true.
In pkms, there is a hash table (Mpackages) that records all of the package information, and each table entry contains applicationinfo information with a dedicated bit in the flags (int) data that is used to represent the persistent. The Getpersistentapplications () function iterates through the table, finds all persistent packages, and returns arraylist<applicationinfo>.
System Application with persistent flag (that is, flags set in Flag_system) is bound to be selected, but if not the system application, it is necessary to further determine whether the current "Safe Mode", once in safe mode, Then even if the app has the persistent property set, it won't be selected.
Applications that have a persistent attribute of true are basically started at the beginning of the system's start-up.
Because the process of starting the process is asynchronous, we need a buffer list (the Mpersistentstartingprocesses list in the code above) to record the Processrecord nodes that are "in the boot state and not completed". Once the target process has been started, the target process will attach the system, so go to AMs's attachapplicationlocked (), in this function, The Processrecord node corresponding to the target process is removed from the mpersistentstartingprocesses buffer list.
How to ensure the persistence of the application (persistent):
We know that the word persistent means "enduring", so what is the meaning of persistent application? Simply put, this application will run stubbornly in the system, from the start of the system to the system shutdown.
To ensure this persistence, the persistent application must be able to automatically restart when an exception occurs. This is achieved in Android. In each activitythread, there will be a binder entity--final Applicationthread Mappthread that specializes in communicating with AMS. The agent interface for this entity in AMS is iapplicationthread.
When AMS executes to attachapplicationlocked (), a Binder obituary listener is registered against the Iapplicationthread interface of the target user process, and once the user process is accidentally suspended, AMS will be able to perceive it in the first time, and take the corresponding measures. If AMS finds an unexpectedly suspended app to be persistent, it will attempt to restart the app.
The code for registering the obituary listener is as follows:
Appdeathrecipient ADR = new Appdeathrecipient (app, PID, thread), Thread.asbinder (). Linktodeath (ADR, 0); App.deathrecipient = ADR;
The thread is the Iapplicationthread agent.
How to make an Android app not be killed android:persistent