This article describes the usage of persistent attribute in Android. Share it for your reference, as follows:
When studying telephony some time ago, I have not found a call to telephony initialization (makeDefaultPhones function in it) under the framework. As a result, after searching globally, I found that it was called in the application PhoneApp (packages/apps/Phone). However, the application PhoneApp is neither awakened by Broadcast nor called by other services. So how does Android start PhoneApp, so the property android:persistent was discovered.
In the definition, application has such an attribute android:persistent. According to the literal meaning, the application is persistent, that is, a resident application. In fact, this is the understanding that the application modified by android:persistent will be started by AM after the system is started.
AM first goes to PM (PackageManagerService) to find the application that has set Android: persistent
public void systemReady(final Runnable goingCallback) { if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { try { List apps = (). getPersistentApplications(STOCK_PM_FLAGS); if (apps != null) { int N = (); int i; for (i=0; i<N; i++) { ApplicationInfo info = (ApplicationInfo)(i); if (info != null && !("Android")) { addAppLocked(info); } } } } catch (RemoteException ex) { // pm is in same process, this will never happen. } } }
If the application that should be modified by Android:persistent is not running at this time, then AM will call startProcessLocked to start the app. The description of startProcessLocked will no longer be described. Another article "How to start a new process for Android?" is introduced in detail (the editor of this English document will not be translated. Interested friends can search and take a look).
The process of starting the app is to start the process corresponding to the package where the app is located.
final ProcessRecord addAppLocked(ApplicationInfo info) { ProcessRecord app = getProcessRecordLocked(, ); if (app == null) { app = newProcessRecordLocked(null, info, null); (, , app); updateLruProcessLocked(app, true, true); } if ((&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { = true; = CORE_SERVER_ADJ; } if ( == null && (app) < 0) { (app); startProcessLocked(app, "added application", ); } return app; }
This introduces how the app is created after the process corresponding to the package where the app is located is started.
From the article "How to start a new process for Android?", we can see that zygote will start its mainThread when creating a new process, so we will analyze the app's create process from the main function of ActivityThread.
There is the following operation in main
(false);
During the attachment process, ActivityThread will attach the corresponding application to the AM and hand it over to the AM for management. A variable needs to be paid attention to here
final ApplicationThread mAppThread = new ApplicationThread();
mAppThread is an ApplicationThread object. mAppThread can be regarded as the core of the main thread of the current process. It is responsible for handling communication between this process and other processes (mainly AM). At the same time, the mAppThread's proxy Binder is passed to AM through attachApplication.
private final void attach(boolean system) { (this); mSystemThread = system; if (!system) { (new Runnable() { public void run() { ensureJitEnabled(); } }); ("<pre-initialized>"); (()); IActivityManager mgr = (); try { (mAppThread); } catch (RemoteException ex) { } } }
In the above attachment code, we follow the attachmentApplication process of IPC calling AM.
During this process, AM calls bindApplication in the IPC communication calling mAppThread;
private final boolean attachApplicationLocked(IApplicationThread thread, int pid) { (processName, != null ? : , providers, , , , , testMode, isRestrictedBackupMode || !normalMode, mConfiguration, getCommonServicesLocked()); updateLruProcessLocked(app, false, true); = = (); }
mAppThread's bindApplication then sends a BIND_APPLICATION message to the handler maintained by ActivityThread through the message mechanism. Let's take a look at the handler's processing of message BIND_APPLICATION maintained by ActivityThread, and will eventually call the handleBindApplication function
You will find that there is a sentence in the handleBindApplication function
(app);
Finally, after a long walk, we called the app's onCreate function to start the application.
PS: For more information about configuration items and their functions, please refer to the online tools of this site:
Android Manifest function and permission description:
http://tools./table/AndroidManifest
For more information about Android related content, please check out the topic of this site:Android database operation skills summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.