Detailed explanation of messenger and Aidl for Android process communication

1. Preface

Mentioned inter process communication (IPC): in the Android system, one process cannot directly access the memory of another process, and some mechanisms need to be provided to communicate between different processes. Android officially launched Aidl (Android interface finalization language), which is based on binder mechanism.

As mentioned in the previous article, there are three communication methods between components and services.

The latter two can communicate across processes and are based on binder mechanism.

2. Use timing

Before deciding what mechanism to use, first understand the application scenario. In Android system, if the communication between components and services is in the same process, the first method is used; For cross process communication, use the second and third methods. The difference between them is that messenger cannot handle multi-threaded concurrent requests.

3. Use of Aidl

Aidl can handle multi-threaded access requests, so the implementation of Aidl must first ensure thread safety.

3.1 creating an. Aidl file

In the project directory in Android studio, you can create a new Aidl file by anti clicking New - > Aidl - > Aidl file. The compiler will automatically create an Aidl file in the app (Shell project) / SRC / main / directory, as well as a new folder. By default, the project package name is the directory where the Aidl file is located.

The directory structure is as follows:

Figure-1 directory structure of Aidl file

It can also be created manually. The package name defined by the Aidl interface can also be different from the project package name. The Aidl file syntax is similar to the Java syntax. The interface name defined by the Aidl must be consistent with the file name, and supports the transfer of user-defined data types. The Parcelable interface needs to be implemented.

IRemoteService.aidl

ParcelableData.aidl

IRemoteServiceCallBack.aidl

The interface defined in the Aidl file supports the following data types:

For the parameters accepted by the method defined in the Aidl file, except the basic data type of Java and the interface defined by Aidl, the other parameters need to mark the direction of the data, in / out / inout. The basic data type and the interface defined by Aidl are used as parameters. The default is in.

The keyword oneway means that the user does not need to wait for a response when requesting the corresponding function, and can call and return directly. It has a non blocking effect. This keyword can be used to declare the interface or method. If the oneway keyword is used in the interface declaration, all the methods declared by the interface adopt the oneway method

After creating the Aidl file, rebuild the project or compile the project with the gradle assemblydebug (or gradle assemblyrelease) instruction to generate specific java code. It depends on the type of build in the debug or release folder under the shell project / build / generated / Aidl / directory, as shown in the figure:

Figure-2 Adil generated code directory

Any modification of the Aidl interface after it is first published must maintain backward compatibility to avoid interrupting the client's use of the service. Because the. Aidl file needs to be copied to other applications to enable other applications to access the service, the support for the original interface must be maintained.

3.2 implementation interface

The Android SDK will generate a. Java file with the same name according to the. Aidl file. The generated interface has an abstract subclass of stub, which implements the interface defined by Aidl and inherits binder.

The specific codes are as follows:

Now mbinder is an instance of the stub class and a binder, which is used for the RPC service defined by the service as the return object instance of the onbind () method.

Precautions for implementing Aidl interface:

3.3 exposing interfaces to clients

After implementing the interface, you need to expose the interface to the client for use by the client. Take the instantiated object of the stub as the return object of the onbind () method in the service.

3.4 client call

Services are provided to third-party applications. Other applications must have interface classes and create the same Aidl file on the client (you can copy it directly).

Code of core connection remote service:

The detail code is relatively long. Paste the project address and click me!!!

4. Use of messenger

Messenger is much more convenient than Aidl, because messenger is a self-contained class in the Android system, and the server and client do not need to create Aidl files.

Messenger will hold a handler, which is used to process the received information and realize communication between the server and passengers through messenger.

4.1 server

Code example:

4.2 client

Core code:

4.3 client sending information

Messenger is used to send messages to the server. The messenger.send (message) method is used. The specific implementation of this method is as follows:

Method calls the mtarget. Send (message) method internally. In Messenger, mtarget is assigned in the constructor and has two constructors.

The first constructor is easy to understand. Mtarget. Send (message) actually adds message to the message queue of the handler passed in by the constructor. This method is used by the server to send information to the passenger in the demo project

Is the second constructor familiar? This is to get the interface defined by Aidl!!! After turning around and returning to Aidl above, the client actually sends information to the server through Aidl, but the Android system helps us make a layer of encapsulation.

Finally, a demo: demo is attached

5. Summary

So far, the process communication commonly used in Android has been summarized, which is an end. Sprinkle flowers!! I hope it will help you in your study, and I also hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>