The application of Android learning--activitymanager and proxy mode
One proxy mode
Intention:
?????? Provides a proxy for other objects to control access to this object.
Applicability:
L? Remote proxy: Provides a local representation of an object in a different address space.
L? Virtual proxies create expensive objects as needed. Use a proxy object as a delegate and create it when you really need it.
L? Protection agent (Protection proxy): Controls access to the original object. The protection agent is used when the object should have different access rights.
L? Smart Reference: Instead of a simple pointer, it performs some additional operations when accessing an object. A reference count that points to the actual object,
This automatically frees the object when it is not referenced, and the smart pointer loads it into memory the first time a persisted object is referenced.
Before accessing an actual object, check to see if it has been locked to ensure that other objects cannot change it
Structure:
An object graph of a possible proxy structure at run time:
????????
Understand:
1) Remote Proxy can hide the fact that an object exists in a different address space. But provide a
Proxy object, which is used as if it were a real object.
2) Virtual Proxy can be optimized, such as creating objects on demand. Copy-on–write.
Reasonable management of the objects being proxied according to actual requirements (create load destroy)
3) Protection proxies and smart reference allow you to access an object with some additional
In-house processing; Set permissions on different accesses to protect the object.
two android in Activitymanager
From the official documentation, you can see the role of Activitymanager:
is to interact with all running acitivity of the system, information about the activity in all of the system's operations (Task,memory,service,app)
Management and maintenance, and provide the appropriate interface for obtaining this information.
?
But the true maintenance of this information is not activitymanager to be responsible, from which many of the interfaces getxxx () can be seen using:
Public list<runningappprocessinfo> getrunningappprocesses () { return Activitymanagernative.getdefault (). getrunningappprocesses ();}
It's all through this activitymanagernative. Getdefault () operation to achieve this information acquisition.
While continuing into the function lookup, it is still not possible to know exactly which class object is implementing the specific operation.
?
So to see clearly activitymanager is really interacting with who,
It is necessary to explore the implementation of the Activity Manager framework related classes, inheritance relationships and hierarchies, and control relationships.
?
three activity manager related class inheritance hierarchy in Android
?????? Look at the class structure diagram as follows:
??????
Iactivitymanager as the public interface of Activitymanagerproxy and Activitymanagernative,
So two classes have some identical interfaces, which can realize the reasonable proxy mode.
Activitymanagerproxy proxy class is the inner class of activitymanagernative;
Activitymanagernative is an abstract class that really works by its subclass Activitymanagerservice (System service component).
?
This is designed to two processes:
Agent Object Establishment: Activitymanagerproxy proxy object creation;
Program execution procedure: How to execute Real object request through proxy object;
?
You can see the proxy class: Use Activitymanagerproxy proxy class, to Proxy activitymanagernative class subclass Activitymanagerservice;
Activitymanagerservice is a system uniform service, which runs in an independent process, and obtains through the system servicemanger;
Activitymanager runs inside a process, Activitymanagerservice runs in another process,
objects in different processes, their addresses are independent of each other, to achieve cross-process object access, need to correspond to inter-process communication rules,
This is the use of the binder mechanism for cross-process communication, so the use of proxy mode here is: remote agent (remoteproxy).
Let's look at these two processes.
?
Four agent implementation process
1 Agent Object creation
?????? It is necessary to execute the proxy class when the Activitymanager getrunningservices executes;
Public list<runningserviceinfo> getrunningservices (int maxnum)
return activitymanagernative. Getdefault ()
getservices (maxnum, 0);
}
Keep looking at activitymanagernative. Getdefault () What did it do:
is actually related to the singleton<iactivitymanager> type of Gdefault object creation;
Private Static Final New Singleton<IActivityManager>() { protected= Servicemanager.getservice ("activity"= Asinterface (b); return am; }};
ServiceManager . GetService ("Activity"), access to the system's "activity" service, ?
All service is registered to ServiceManager for unified management.
This creates a local proxy object Activitymanagerproxy instance of the Activitymanagerservice instance. Singleton is a generic, single-instance template class.
?????? activitymanagernative. Getdefault returns a public interface Iactivitymanager type for this proxy object, which allows you to invoke the operation method of the remote object locally.
??????
2 Execution Process
?????? This execution process is designed to the execution of the Activitymanager framework; simply take a look at the execution of this getservices.
??????
This figure shows that the entire client access to the service is accessed through proxy object proxies for the service.
The mode of service access in Android is in client/server mode;
The client actually accesses the service through the proxy mode, which is accessed through the proxy object of the service's establishment agent.
It is also shown here that if the service component of the remote terminal associated with Activitymanager can be arbitrarily changed and replaced, it will still not affect the use of the local side.
?
The framework of Acitivitymanager is very complex, here is mainly to learn the application of proxy mode.