Service side:
The first is to write a aidl file, note that Aidl only supports methods, cannot define static members, and methods do not have modifiers such as public; the Aidl Run method has any type of parameter and return value, in the Java type, the following types do not need to import packages (imports) when used , basic data types, String, Map, List. Of course, in order to avoid errors, it is recommended that you import the package as long as you use it.
Then start a service on the server and register to write an arbitrary class that implements the Java interface for Aidl file generation stub!
Finally, instantiate any of your classes in the service and return the objects of your class on the Onbind (Intent a) Method!
Client:
First of all, you copy the Aidl file on the server, and make sure the package name is the same.
Then call the Bindservice method to bind the method you created on the server Mcontext.bindservice (intent, mserviceconnection, 0);
Where Mserviceconnection is the focus:
Private Serviceconnection mserviceconnection = new Serviceconnection () {
@Override public
Void onserviceconnected (componentname name, IBinder service) {//when connecting to the server, call
Mservice = IRemoteService.Stub.asInterface ( Service);
}
The @Override public
void onservicedisconnected (componentname name) {//is not connected to invoke
Mservice = null;
}
;
Where Mservice is the Aidl object of the server, which we can assign to the service-side method
The intent must pay attention to the need to provide a intent-filter to match the request is legitimate, so we access the service in the client, we must also pass the matching action intent Oh.
The above content is small series to introduce the Android aidl communication mechanism, I hope to help you!