On Android Aidl Communication Mechanism

  • 2021-06-28 14:07:59
  • OfStack

Server:

First, write an aidl file, noting that AIDL only supports methods, cannot define static members, and methods cannot have modifiers like public, etc.The AIDL method has any type of parameters and return values. In the java type, the following types do not need to be imported into the package (import). The basic data types, String, Map, List. Of course, to avoid errors, it is recommended that you import the package whenever you use it.

Then start a service on the server side and register it, write an arbitrary class to implement JAVA interface Stub generated by AIDL file!

Finally, instantiate any of your classes in service and return the object of any of your classes on the onBind (Intent a) method!

Client:

First copy your aidl file on the server completely into one copy, and make sure the package name is the same

Then call the bindservice method to bind the method mContext.bindService (intent, mServiceConnection, 0) that you created on the server side;

mServiceConnection is the focus:


private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {// Called when connecting to a server 
mService = IRemoteService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {// Not even called 
mService = null;
}
};

mService is the aidl object on the server side, through which we can assign values to the methods on the server side.

The Intent passed in must be aware that an intent-filter must be provided to match the validity of the request, so when clients access the service, we must also pass an Intent containing the matching action.

The above content is the Android Aidl communication mechanism introduced to you by this site, and I hope it will help you!


Related articles: