preface

As we all know, in Android, data between processes is isolated from each other. Therefore, inter-process traffic needs to be carried out through IPC(inter-process communication). Binder is a way of IPC in Android. The underlying principle of Binder mechanism implementation is too complicated to be described here. The following mainly explains the process of interprocess communication through AIDL in Android.

An overview of the

In development projects, we sometimes have an Activity or service set up in a different process for memory or other reasons. If we use event distribution, some places will not receive messages because the process is different. At this point, we will consider whether we can first distribute to different processes through IPC, and then distribute to each process. The following is a project to implement and understand the use of AIDL.

GitHub

The code can be found here

AIDL development

AIDL create

First we create AIDL File by “right click” New “AIDL” AIDL File, after the creation of the main File directory to generate an AIDL folder, and under the folder to generate the same package name as the project directory, the newly created AIDL File is in it.

Custom type creation

In ALDL, the following data types are supported:

Byte, char, short, int, long,float, double, Boolean, String, CharSequence List, Map(lsit and Map types must be aiDL-supported types)Copy the code

Custom creation is required if custom types are to be supported. First we create a.java file in the Java directory. It then inherits the Parcelable interface. It is recommended to set parameters first and then add relevant methods through Implement Methods.

Then create a.aidl file under the same package name in the aidl file directory. Ps: Ensure that the package names and file names are the same.

After that we can use our custom types in AIDL.

reference

Custom type references and AIDL interface writing.

A few things to note here:

  • Because it is an AIDL file, the import of the package name is written manually.
  • The interface methods set up in the file are the methods that can be called later when aiDL uses them.
  • In, out, and inout are the directional tags in AIDL that represent how data flows through the parameter object when used.
compile

After the above file is created, you can use the menu bar “Build” Make Project to compile it. After the compilation is successful, a file with the same name will be generated in the Build >generated> AIdl >debug> package name path.

call

Then we can make the call in the service.

AIDL use

##### Multi-process Settings Create two activities configured to be different processes

Connect to the AIDL service in the Activity
  • Service binding is done through the bindService() method.
  • Get the IBinder in the ServiceConnection callback.
  • Get the ITermiteAidlInterface proxy from IBinder and set the callback
AIDL data send test

Send objects in TwoActivity through the sendEventMessage() method

At this point, the underlying logic for AIDL usage and inter-process event distribution has been implemented.