Android learning-Application of activitymanager and proxy Modes

Source: Internet
Author: User

 

Android learning-Application of activitymanager and proxy Modes

One proxy Mode

Intent:

Provides a proxy for other objects to control access to this object.

Applicability:

L remote proxy: provides a local representation of an object in different address spaces.

L virtual proxy creates objects with high overhead as needed. Use a proxy object as the representative and create it as needed.

L protection proxy: controls access to the original object. Protection proxy is used when the object should have different access permissions.

L smart reference: instead of simple pointers, it performs some additional operations when accessing objects. The reference count pointing to the actual object,

In this way, when the object is not referenced, it can be automatically released. When the smart pointer references a persistent object for the first time, it is loaded into the memory.

Before accessing an actual object, check whether it is locked to ensure that other objects cannot change it.

Structure:

An object diagram of a possible proxy structure at runtime:

Understanding:

1) remote proxy can hide the fact that an object exists in different address spaces. However

Proxy objects, just like using real objects.

2) virtual proxy can be optimized, such as creating objects as required. Copy-on-write.

Manage the proxy objects based on actual needs (create, load, and destroy objects)

3) Protection proxies and smart reference both allow some additional

Set different access permissions to protect objects.

2. activitymanager in Android

The functions of activitymanager are as follows:

Is to interact with all the running acitifiers in the system,Information about all running activities in the system (task, memory, service, APP)

Provides corresponding interfaces for obtaining such information.

 

However, the actual maintenance of the information is not the responsibility of activitymanager. The following figure shows that all of the interfaces getxxx () are used:

PublicList <runningappprocessinfo>Getrunningappprocesses (){ReturnActivitymanagernative. getdefault (). getrunningappprocesses ();} 

All through thisActivitymanagernative.Getdefault() To obtain the information.

Although you continue to search for functions, you still cannot know which class object is performing specific operations.

 

So we need to know who activitymanager really interacts,

You need to explore the implementation of activity manager framework related classes, inheritance relationships, hierarchies and control relationships.

 

3. Inheritance hierarchies of activity manager classes in Android

The class structure is as follows:

Iactivitymanager is the public interface of activitymanagerproxy and activitymanagernative,

Therefore, the two classes have partially identical interfaces and can implement a reasonable proxy mode;

The activitymanagerproxy proxy is the internal class of activitymanagernative;

Activitymanagernative is an abstract class and its subclass actually plays a role.Activitymanagerservice (System Service component).

 

Two processes are designed here:

Proxy object creation: creates an activitymanagerproxy object;

ProgramExecution Process: how to execute real object requests through proxy objects;

 

It can be seen that the agent class uses the activitymanagerproxy proxy class to represent the activitymanagernative class subclass activitymanagerservice;

Activitymanagerservice is a unified system service and runs in an independent process. It is obtained through the system servicemanger;

Activitymanager runs in one process, and activitymanagerservice runs in another process,

The addresses of objects in different processes are independent from each other. Cross-process object access requires rules for inter-process communication,

The binder mechanism is used to implement cross-process communication. Therefore, the use of the proxy mode here belongs:Remoteproxy).

Let's take a look at these two processes.

 

4. Proxy implementation process

1. Create a proxy object

It is executed by the proxy class when getrunningservices of activitymanager is executed;

Public list <runningserviceinfo> getrunningservices (INT maxnum)

ReturnActivitymanagernative.Getdefault()

Getservices(Maxnum, 0 );

}

ContinueActivitymanagernative.Getdefault() What did you do:

It is actually about creating a gdefault object of the singleton <iactivitymanager> type;

 
Private Static FinalSingleton <iactivitymanager> gdefault =NewSingleton<Iactivitymanager>(){ProtectedIactivitymanager create () {ibinder B= Servicemanager. getservice ("activity"); Iactivitymanager AM=Asinterface (B );ReturnAm ;}};

Servicemanager.Getservice("Activity"); obtain the system's "activity" service, 

All services are registered to servicemanager for unified management.

In this way, a local proxy instance for the activitymanagerservice instance is created. Singleton is a common Singleton template class.

Activitymanagernative. Getdefault returns the iactivitymanager type, which is the public interface of this proxy object. You can call the operation method of the remote object locally.

2. Execution Process

The execution process is designed for the activitymanager framework. Let's take a look at the execution process of getservices.

This figure shows that the entire client accesses the service through the proxy object proxy of the service.

In Android, the service access mode is in Client/Server mode;

In fact, the client accesses the service through the proxy object that establishes a proxy for the service-proxy mode.

We can also see that if the service components of the remote end of activitymanager can be changed or replaced at will, the usage of the local end will not be affected.

 

The acitivitymanager framework is very complex. It is mainly used to learn the application of the proxy mode.

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.