SoFunction
Updated on 2025-04-09

Exploration of inter-process communication implementation in Android AIDL

Preface: 

The previous summary is to share data between programs. You can use ContentProvider or SharedPreference. So how do you share memory between processes? Memory cannot be shared between processes in the Android system, so some mechanisms need to be provided to communicate data between different processes.

In order to enable other applications to access the services provided by this application, the Android system adopts remote procedure call (RPC) method to implement it. Like many other RPC-based solutions, Android uses an interface definition language (Interface Definition Language, IDL) to expose the service's interface. We know that 3 of the 4 Android application components (Activity, BroadcastReceiver and ContentProvider) can all be accessed across processes, and the other Android application component Service can also be accessed. Therefore, this kind of service that can be accessed across processes can be called an AIDL (Android Interface Definition Language) service.

Next, let’s take a closer look at the actual implementation:

1.) First create a new aidl file

interface ITestInterface {
   //Get the process ID  int getProcessId();
  //Processing strings  String dealString( String srcString);
  //Add string  String appendString( String srcString);

  void addPerson(in Person person);

  List<Person> getPersons();
} 

aidl syntax explanation:
• The declaration function is basically the same as Java, and can pass parameters and return values, parameters and return values
• Parameters and return values ​​Basic data types of Java programming language (int, long, char, boolean, etc.), String and CharSequence, collection interface types List and Map, other AIDL interface types, custom objects that implement Parcelable interface
• Direction indication    When using aidl to transmit data, for non-primitive data types, they are not of String and CharSequence types (i.e. Parcelable types), there is a direction indication, including in, out and inout.
• AIDL only supports interface methods and cannot expose static variables.

2.) Server side implementation interface

  private final  mBinder = new () {
    public int getProcessId(){
      ("TestService","TestService Thread: " + ().getName());
      ("TestService","TestService getProcessId()");
      return ();
    }
    //Processing strings    public String dealString( String srcString)
    {
      return srcString+srcString;
    }

    //Add string    public String appendString( String srcString)
    {
      return srcString+srcString;
    } 

3.) Client acquisition interface

  private ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
      ("TestService","TestService onServiceDisconnected()");
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      iTestInterface =  (service);
      try {
        ("TestService","TestService onServiceConnected()");
        int remoteId=();
        ("TestService","TestService remoteId---->"+remoteId);
        int currentPid = ();
        ("TestService","TestService currentPid---->"+currentPid);
        ("TestService","TestService dealString---->"+("Remote Service"));
        ("TestService","TestService appendString---->"+("Remote Service"));
      } catch (RemoteException e) {
        ();
      }
    }
  }; 

4.) Call/pass data through IPC

        int remoteId=();
        ("TestService","TestService remoteId---->"+remoteId);
        int currentPid = ();
        ("TestService","TestService currentPid---->"+currentPid);
        ("TestService","TestService dealString---->"+("Remote Service"));
        ("TestService","TestService appendString---->"+("Remote Service")); 

5.) Service declaration and binding/unbinding

Statement:

    <service
      android:name=".TestService"
      android:enabled="true"
      android:exported="true"
      android:label="remoteService"
      android:process=":remote">
      <intent-filter android:priority="1000">
        <category android:name="" />
        <action android:name="" />
      </intent-filter>
    </service> 

Bind:

 Intent intent = new Intent("");
 (getPackageName());//Here you need to set the package name of your application bindService(intent, connection, Context.BIND_AUTO_CREATE); 

Unbinding: unbindService(connection);

6.) Access permissions are the same as Service

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.