Android talks about the Aidl communication mechanism and androidaidl
Server:
First, write an aidl file. Note that AIDL only supports methods, does not support defining static members, and does not support public modifiers; the AIDL running method has any type of parameters and return values. In java, the following types do not need to be imported (import). The basic data types include String, Map, and List. to avoid errors, we recommend that you import the package as long as you use it.
Start a service on the server and register it. Compile a JAVA interface Stub that can be generated by any class to implement AIDL file!
Finally, instantiate any of your classes in the service and return the objects of any of your classes on the onBind (Intent a) method!
Client:
First, copy the complete aidl file on the server end and ensure that the package name is the same.
Then call the bindservice method to bind the method mContext. bindService (intent, mServiceConnection, 0) You created on the server );
Among them, mServiceConnection is the focus:
Private ServiceConnection mServiceConnection = new ServiceConnection (){
@ Override
Public void onServiceConnected (ComponentName name, IBinder service) {// called when connecting to the server
MService = IRemoteService. Stub. asInterface (service );
}
@ Override
Public void onServiceDisconnected (ComponentName name) {// No connected call
MService = null;
}
};
MService is the aidl object of the server. You can assign values to the methods of the server.
The uploaded Intent must provide an intent-filter to match whether the request is valid. Therefore, when the client accesses the service, it must also pass the Intent that matches the action.