Android Persistent Attribute Usage _android

Source: Internet
Author: User

This example describes the use of persistent properties in Android. Share to everyone for your reference, specific as follows:

During the study of telephony, the telephony initialization (Makedefaultphones function in Phonefactory.java) has not been found under the framework of the previous period. Results The global search was later found to be called in Application Phoneapp (Packages/apps/phone). But application Phoneapp was neither awakened by broadcast nor invoked by other service, so it was the way that Android started Phoneapp, so it found the attribute android:persistent.

In the Androidmanifest.xml definition, application has such an attribute android:persistent, which is understood literally to mean that the application is sustainable, and that is, a resident application. In fact, this is the understanding that the application modified by Android:persistent will be started by am when the system is started.

Am first to go to the PM (packagemanagerservice) to find the application set up Android:persistent

public void Systemready (final Runnable goingcallback) {
  if (mfactorytest!= systemserver.factory_test_low_level) { C2/>try {
      List apps = Appglobals.getpackagemanager ().
        Getpersistentapplications (stock_pm_flags);
      if (apps!= null) {
        int N = Apps.size ();
        int i;
        For (i=0 i<n; i++) {
          applicationinfo info
            = (applicationinfo) apps.get (i);
          if (info!= null &&
              !info.packagename.equals ("Android")) {
            addapplocked (info);
          }
        }
      }
    the catch (RemoteException ex) {
      //PM is in same process, this'll never happen.
    }
  }
}

If the application that is Android:persistent modified is not running at this time, then am will invoke startprocesslocked to launch the app, about startprocesslocked no longer described, and another article "How to Start" A new process for Android? In a detailed introduction (This English document small series will not translate, interested friends can search to see).

The app's startup process is to start the package corresponding process of the app.

Final Processrecord addapplocked (applicationinfo info) {
    Processrecord app = getprocessrecordlocked ( Info.processname, info.uid);
    if (app = null) {
      app = newprocessrecordlocked (null, info, NULL);
      Mprocessnames.put (Info.processname, Info.uid, app);
      Updatelruprocesslocked (app, True, true);
    }
    if (info.flags& (applicationinfo.flag_system| applicationinfo.flag_persistent)
        = = (applicationinfo.flag_system| applicationinfo.flag_persistent)) {
      app.persistent = true;
      App.maxadj = Core_server_adj;
    }
    if (App.thread = = null && mpersistentstartingprocesses.indexof (APP) < 0) {
      Mpersistentstartingprocesses.add (app);
      startprocesslocked (App, "added application", app.processname);
    }
    return app;
}

Describes how the app was create after the package of the app was started.

From the article "How to start a new process for Android", Zygote will start its mainthread when creating new processes. Android.app.ActivityThread, so we then analyze the app's create process from the main function in Activitythread.

The following action is available in main

Thread.attach (FALSE);

During the attach process, Activitythread will attach the corresponding application to AM, and I will manage it with AM. Here you need to pay attention to a variable

Final Applicationthread mappthread = new Applicationthread ();

Mappthread is a Applicationthread object, and Mappthread can be viewed as the core of the current process's main thread, which is responsible for handling communication between this process and other processes (primarily AM). The Mappthread Agent Binder is passed to AM by Attachapplication.

Private final void Attach (Boolean system) {
    sthreadlocal.set (this);
    Msystemthread = System;
    if (!system) {
      Viewroot.addfirstdrawhandler (new Runnable () {public
        void run () {
          ensurejitenabled ();
        }
      });
      Android.ddm.DdmHandleAppName.setAppName ("<pre-initialized>");
      Runtimeinit.setapplicationobject (Mappthread.asbinder ());
      Iactivitymanager mgr = Activitymanagernative.getdefault ();
      try {
        mgr.attachapplication (mappthread);
      } catch (RemoteException ex) {
      }
    }
}

In the attach code above, we look down the attachapplication process of the IPC calling AM.
In this process, AM is invoked to the bindapplication of the IPC communication call Mappthread;

Private Final Boolean attachapplicationlocked (Iapplicationthread thread,
  int pid) {
  thread.bindapplication ( ProcessName, app.instrumentationinfo!= null
      ? App.instrumentationInfo:app.info, providers,
      App.instrumentationclass, App.instrumentationprofilefile,
      app.instrumentationarguments, App.instrumentationwatcher, Testmode,
      isrestrictedbackupmode | |!normalmode,
      mconfiguration, Getcommonserviceslocked ());
  Updatelruprocesslocked (app, False, true);
  APP.LASTREQUESTEDGC = App.lastlowmemory = Systemclock.uptimemillis ();
}

The Mappthread bindapplication sends bind_application messages to the handler that Activitythread itself maintains through the message mechanism. The following is a look at the processing of message bind_application by the handler Activitythread itself, which will eventually be invoked to the Handlebindapplication function

You'll find that there's a sentence in the Handlebindapplication function

Minstrumentation.callapplicationoncreate (APP);

Finally, after a great circle, we called the app's OnCreate function to start the application.

PS: More about Androidmanifest.xml configuration items and their functions can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.