Android binder inter-process communication mechanism 6-JAVA interface of inter-process communication mechanism of Binder, android6-binder

Source: Internet
Author: User

Android binder inter-process communication mechanism 6-JAVA interface of inter-process communication mechanism of Binder, android6-binder

JAVA layer interface for inter-process communication between Binder, mainly through the JNI method to call the C/C ++ interface of the Binder Library

On the JAVA layer, a Service component is called a JAVA Service, and a Service component proxy is called a JAVA Service proxy.

1. ServiceManager Java layer proxy (ServiceManagerProxy) acquisition process.

I personally feel that the JAVA service of ServiceManager on the Java layer and the implementation structure of Java Service proxy are still very similar, as follows:

Java service class diagram:

  

Class relationship diagram of java Service Proxy:

  

The ServiceManager proxy object is obtained through getIsServiceManager of the ServiceManager class. The asInterface of ServiceManagerNative encapsulates a BinderProxy into ServiceManagerProxy.

The BinderProxy is obtained by calling android_ OS _BinderInternal_getContextObject through JNI. The Java-layer BinderProxy object is created by this function and saved in the global variable gBinderProxyOffsets.

Ii. Definition and resolution of Java service interfaces.

The interface is defined in the. AIDL file using the aidl language. A Java file with the corresponding interface name will be compiled. Where

I *** Service. Stub describes a Java Service and needs to be inherited to implement specific services.

OnTransact is used to implement inter-process communication.

AsInterface packs a BinderProxy as I *** Service. Stub. Proxy.

I *** Service. Stub. Proxy describes a Java Service Proxy object

Iii. Java Service Startup Process

1. Initialization is mainly to call init in its parent class Binder, and then create JavaBBinderHolder in the C ++ layer through android_ OS _Binder_init, and save it in gBinderOffsets. mObject.

2. register the service to ServiceManager and call the addService of ServiceManager.

4. Obtain the Java Service proxy object

Use ServiceManagerJava Service proxy's getService

V. Java service calls

The Service proxy actually exposes the service interface and calls the corresponding function.

To sum up:

In android, the Binder communication mechanism is divided into three layers:

Java-layer encapsulated Binder: JNI is called. In fact, a communication request is sent to the local Binder object through the Binder proxy object in the C ++ layer.

========================================================== ======================================

Service implementation at the C ++ layer: The Binder proxy object requests specific operations to the Binder local object through the Binder driver. The Binder local object

You may also call the Java-layer service to complete the function.

========================================================== ======================================

The Binder driver is actually a tool for proxy processes to communicate with service processes. A microphone is not responsible for specific services and its implementation is essentially

Write the defined command protocol code and return protocol code to the kernel buffer to coordinate the servers at the C ++ and Java layers to provide services.

    

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Why does android not directly use the linux shared memory mechanism for inter-process communication, but use AIDL to publish service interfaces?

Android seems to be using the Binder driver at the underlying layer for sharing. This driver can avoid many problems with shared memory in Linux. I have not analyzed the details.

What is the communication (IPC) between android processes?

Android is based on the Linux kernel. Therefore, linux supports IPC and android. Such as Named Pipes and shared memory. In addition, android uses its own unique IPC binder, which is mainly used for remote calls between two processes. However, this involves how to remotely call the API to PASS Parameters and return results. This requires the caller to package and package the data, which is a tedious process. Therefore, android introduces aidl (android interface description launguage). The developer defines aidl. android will produce the stub code according to the description of aidl to help the caller package and unpack the data. What developers need to do Is inherit the stub code and implement functions in the stub code. These functions are defined in aidl.

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.