Measure the test taker's understanding about the inter-process communication principle of Android system (I) -- Agent Mode in RPC-RMI-RPC

Source: Internet
Author: User
Understanding the communication principle between processes in Android (I) ---- proxy mode in RPC
In the Android system, inter-process communication is implemented through a lightweight RPC (Remote
Procedure Call remote process call) and aidl (Android interface definination
Language) to generate code that can be accessed between two processes. RPC is implemented in the interface mode. The client and called implementation are implemented in the proxy mode.
Based on the RMI and proxy modes of Java, to flexibly master this lightweight solution, it is necessary to straighten out these basic knowledge. Here we first understand the basis of the proxy mode, for more information about the proxy mode, you can use the Mind Map below: here we use a code example to describe the actual use: 1. abstract class role code package

Com. magc. proxy;

/**

* Common abstract classes of proxy roles and real roles

*/

Public
Abstract
Class
Role {

//

As a common interface between the proxy role and the real role, the proxy role can replace the real role to provide services.

Public
Abstract
Void

Service (string user_id );

}

2. Real role code package
Com. magc. proxy;
/**


4
*
@ Author
Magc

5
* Real Angle

6
* External access is not allowed

7
*

8

*/


9
Public

Class
Realrole
Extends
Role {

10


11

/*
(Non-javadoc)

12
* @ See COM. magc. Proxy. Role # Service ()

13
* Provide services

14

*/


15
@ Override

16

Public

Void
Service (string user_id ){

17
System. Out. println (
"
Real roles serve you ......
"
);

18
}

19

//
Verify user identity


20

Public

Boolean
Checkuser (string user_id)

21
{

22

Return

True
;

23
}

24


25
} 3. Proxy proxyrole code: 1
Package
Com. magc. proxy;

2


3
/**


4
*
@ Author
Magc

5
* Proxy role

6
* Develop interfaces for clients

7
* You can directly reference a real role instance and forward client requests to a real role instance.

8
* Some additional operations can be added before or after the request is forwarded.

9

*/


10
Public

Class
Proxyrole
Extends
Role {

11

Private
Realrole
=

Null
;

12


13

/*
(Non-javadoc)

14
* @ See COM. magc. Proxy. Role # Service ()

15

*/


16
@ Override

17

Public

Void
Service (string user_id ){

18
System. Out. println (
"
The proxy role serves you ......
"
);

19

//
Create a real role instance only when necessary


20
Realrole
=

New
Realrole ();

21

//
Additional operations: Authentication


22
System. Out. println (
"
Identity Verification ......
"
);

23

If
(
!
Realrole. checkuser (user_id ))

24

Return
;

25
System. Out. println (
"
Find a real role instance to help with the transaction ......
"
);

26
Realrole. Service (
"
Magc
"
);

27


28
System. Out. println (
"
Thank you for choosing ......
"
);

29
}

30


31
} 4. Test class roletest class code 1
Package
Com. magc. proxy;

2


3
/**


4
*
@ Author
Magc

5
* Agent Mode Test

6
* Acts as a client to request and call the proxy interface.

7
* The client can only access the proxy class, but cannot access the real role class.

8

*/


9
Public

Class
Proxytest {

10


11

/**


12
*
@ Param
ARGs

13

*/


14

Public

Static

Void
Main (string [] ARGs ){

15


16
Proxyrole proxy
=

New
Proxyrole ();

17
Proxy. Service (
"
Magc
"
);

18
}

19


20
} Run the test class. The output result on the console is as follows: errors are inevitable due to the rush of time. I hope I can give some inspiration to you. I am here to give some suggestions. (2)

To understand the principles of RPC for lightweight solutions in Android systems, we need to review RMI (Remote Method Invocation) in Java, an easy-to-use pure Java solution (used to implement distributed applications ). RMI-related knowledge can be summarized as follows:

RPC in Android is also based on the RMI solution in Java. Here we will learn more about the implementation process of RPC.

The RPC mechanism in Android is used to implement a process to use remote objects in another process. It uses the android aidl (Interface Definition Language) to enable users to easily
Define an interface as the specification, and use a remote service as the proxy
The client obtains the remote object during binding to the remote service and then uses the object. See:

Supplement: The other purpose of RPC is to declare only interfaces and methods on the client and hide the specific implementation class for the client to directly obtain the interface instance.

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.