interprocess communication in the Android system is via a lightweight RPC (remote Procedure call remoting process) and Aidl (Android Interface definination Language) Specification to generate code that can access each other between two processes. Where RPC is implemented as an interface, between the client and the called implementation is implemented through the proxy mode, which is based on the Java RMI and proxy mode, to flexibly grasp this lightweight solution, it is necessary to re-rationalize these basic knowledge, Here we first understand the relevant basis of the proxy pattern, the knowledge of the proxy pattern, can be expressed in the following mind guide: Here is a code example to illustrate the actual use: 1, abstract class role code
1 package com.magc.proxy;
3/**
4 * @author MAGC
5 * Common abstract class for proxy roles and real roles
6 */
7 public abstract class Role {
8
9 //As a proxy role and the real role of the common interface, convenient proxy role to replace the real role to provide services
Ten public abstract void service (String user_id);
12}
2. Real role class Realrole code
1 package com.magc.proxy;
3/**
4 * @author MAGC
5 * Real character class
6 * External is inaccessible
7
8 */
9 public class Realrole extends Role {
One /* (Non-javadoc)
* @see Com.magc.proxy.role#service ()
* Services available
*/
@Override
Public void Service (String user_id) {
System.out.println ("Real role for You ...");
(+ }
//Verify user identity
Public Boolean CheckUser (String user_id)
{
return true;
(+ }
25}
3, proxy class Proxyrole code:
1 package com.magc.proxy;
3/**
4 * @author MAGC
5 * Proxy role Class
6 * develop its interface to the client
7 * Internal can refer to real role instances directly, transfer client's request to real role instance
8 * Additional actions can be added before or after a forward request
9 */
public class Proxyrole extends Role {
One private realrole realrole = null;
/* (Non-javadoc)
* @see Com.magc.proxy.role#service ()
*/
@Override
+ Public void Service (String user_id) {
System.out.println ("Agent Role for You ...");
to create a real-world role instance when needed
realrole = new Realrole ();
+ //Add extra action: Verify identity
System.out.println ("Verify identity ...");
if (!realrole. CheckUser (user_id))
return;
System.out.println ("Go to the real role instance to help with the transaction ...");
realrole.service ("MAGC");
27
System.out.println ("Thank you for coming ...");
}
31}
4, test class Roletest class code
1 package com.magc.proxy;
3/**
4 * @author MAGC
5 * Proxy mode Test class
6 * as a client to request an interface to invoke the proxy class.
7 * clients can access only proxy classes, not real role classes
8 */
9 public class Proxytest {
/**
* @param args
* *
Public static void Main (string[] args) {
Proxyrole proxy = new Proxyrole ();
proxy.service ("MAGC");
(+ }
20}
Run the test class, the console output is: Because of the time rush, the mistake unavoidably, hoped can give everybody to have the revelation, also is in here the suggestion, hehe
Understanding the inter-process communication principle of the Android system (i)----proxy mode in RPC