Android Framework Activitymanagerservice

Source: Internet
Author: User

Activitymanagerservice


Activitymanagerservice (AMS) manages all the components (Activity, service, etc.) that are created in the application, and the information for each component is within the scope of the management, including the memory release unspoken rules. In order to achieve the management of the components, the state change of each component needs to notify AMS, and the cross-process communication (IPC) between components is also built by AMS. So AMS is very important, everywhere in the source code.

First look at the AMS static class structure diagram:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/26/wKioL1R5twzA9uG6AAV6ZK-MJdo297.jpg "title=" Xxxxx.jpg "alt=" Wkiol1r5twza9ug6aav6zk-mjdo297.jpg "/>

Example: Interaction of Class diagram objects when initiating activity


1. Start activity to see the following source code:

Instrumentation.activityresult AR = minstrumentation.execstartactivity (this, Mmainthrea D.getapplicationthread (), Mtoken, this, intent, requestcode);


Instrumenttaion execstartactivity Source code is as follows:


int  result = activitymanagernative.getdefault ()                  .startactivity (whothread, intent,                          intent.resolvetypeifneeded (Who.getcontentresolver ()),                         null,  0, token, target != null ? target.mEmbeddedID : null,                          requestcode, false, false, null, null, false); 


What does Activitymanagernative.getdefault () get in the code? See the following source code:


 static public iactivitymanager getdefault ()  {         return gdefault.get (); }private static final singleton< Iactivitymanager> gdefault = new singleton<iactivitymanager> ()  {         protected iactivitymanager create ()  {             ibinder b = servicemanager.getservice (" Activity ");            if  (false)  { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.V (" Activitymanager ", " default service binder =  " + b);             }             iactivitymanager am =&nbsP;asinterface (b);            if  (false)   {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.V (" Activitymanager ", " default service =  " + am);             }             return am;        }    };static  Public iactivitymanager asinterface (ibinder obj)  {         if  (obj == null)  {             return null;        }         IActivityManager in =              (Iactivitymanager) oBj.querylocalinterface (Descriptor);        if  (in !=  NULL)  {            return in;         }        return new  activitymanagerproxy (obj);     }


The singleton in the code can be seen as a template for a singleton, and Getdefault () returns Gdefault.get () as the result of returning singleton.create, in the Create method through Servicemanager.getservice ("Activity") is the object of the AMs object, from the class diagram can be seen that AMS is also a IBinder object, After getting the AMS object, the Asinterface method is called to use the remote proxy as: Activitymanagerproxy.


The conclusion is that Activitymanagernative.getdefault () gets the Activitymanagerproxy object, followed by the call to Activitymanagerproxy.startactivity. Check the source code:


 public int startactivity (iapplicationthread caller, intent intent,             String resolvedType, Uri[]  granteduripermissions, int grantedmode,             IBinder resultTo, String resultWho,             int requestCode, boolean onlyIfNeeded,             boolean debug, String profileFile,  parcelfiledescriptor profilefd,             Boolean autostopprofiler)  throws RemoteException {         parcel data = parcel.obtain ();         Parcel reply = parcel.obtain ();Nbsp;       data.writeinterfacetoken (Iactivitymanager.descriptor);         data.writestrongbinder (caller != null ?  Caller.asbinder ()  : null);         intent.writetoparcel (data,  0);         data.writestring (Resolvedtype);         data.writetypedarray (granteduripermissions, 0);         data.writeint (Grantedmode);        ...         mremote.transact (Start_activity_transaction, data, reply,  0);         reply.readexception ();         int result = reply.readint ();         Reply.recycle ();          data.recycle ();        return  result;    }



It can be seen that activitymanagerproxy is responsible for packaging the parameters to parcel and calling Mremote.transact to send start_activity_transaction instructions, when the Mremote object is both the AMS object. The AMS object inherits Activitymanagernative,activitymanagernative implements the Ontransact, is responsible accepts the mremote.transact sends the parcel package. And according to code namely: Start_activity_transaction, call the implementation of the AMS StartActivity method. By then, Activitymanagerproxy.startactivity has been called to the AMS startactivity.


Look at the following timing diagram:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/27/wKiom1R5tsnQ43dQAAFQ1qQ3G8I030.jpg "title=" 1.jpg " alt= "Wkiom1r5tsnq43dqaafq1qq3g8i030.jpg"/>

Summary: This **native.java, **proxy.java the remote proxy mode, the proxy is responsible for packaging parameters and send, native is responsible for accepting the package and invoke the concrete implementation. such as the source of Contentprovidernative and Contentprovierproxy.

This article is from the "Itlife" blog, make sure to keep this source http://xwteacher.blog.51cto.com/9653444/1584553

Android Framework Activitymanagerservice

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.