Refs:http://developer.android.com/guide/components/aidl.html
aidl (Android Interface definition Language) is just like any other interface definition language. It allows you to define the interfaces of the server and client programs to achieve cross-process communication (IPC) purposes .
Attention:
1. in the case of multi-process multithreading, we only use Aidl
2. When a single process is used, the interface is defined in a way that implements the Binder class
3. If you have only a cross-process, but do not need to Hishinuma multithreading, use Messenger
First, create a aidl file
Imyaidlinterface.aidlpackage com.example.shanwu.interprocesscomm;//Declare Any non-default types here with Import Statementsinterface imyaidlinterface { int getpid (); String getservicename (); void Makesingingservice ();}
The AIDL supports the following data types as parameters and callback types for interface methods:
1. All primitive data type
2. String
3. Charsequence
4. List
5. Map
The aidl file is stored under/SRC, and during the compilation process, it automatically generates a. java file, which includes a subclass named Stub , which is an abstract implmentation of a Aidl interface class and has all the Aidl interface classes There is a declaration method as follows, which we will explain in the back, so that we have their own realization, and not rely on the ability of Aidl, as follows:
/* * This file is auto-generated. Do not MODIFY. * Original File:/home/shanwu/github/interprocesscommpractice/interprocesscomm/app/src/main/aidl/com/example/ Shanwu/interprocesscomm/imyaidlinterface.aidl */package com.example.shanwu.interprocesscomm;//Declare any Non-default types here with Import Statementspublic interface Imyaidlinterface extends Android.os.IInterface {/** * Local-side IPC implementation stub class. */public static abstract class Stub extends Android.os.Binder implements Com.example.shanwu.interprocesscomm.IMyAidlIn Terface {private static final java.lang.String descriptor = "Com.example.shanwu.interprocesscomm.IMyAidlInterface" ; /** * Construct The stub at attach it to the interface. */Public Stub () {this.attachinterface (this, descriptor); }/** * Cast an IBinder object to an Com.example.shanwu.interprocesscomm.IMyAidlInterface interface, * Generating a proxy if Needed. */public static com.example.shanwu.interprocesscomm.IMyAidlInterface asinterface (Android.os.IBinder obj ) {if ((obj = = null)) {return null; } android.os.IInterface iin = Obj.querylocalinterface (descriptor); if ((iin! = null) && (iin instanceof Com.example.shanwu.interprocesscomm.IMyAidlInterface)) {RET Urn ((com.example.shanwu.interprocesscomm.IMyAidlInterface) iin); } return new Com.example.shanwu.interprocesscomm.IMyAidlInterface.Stub.Proxy (obj); } @Override Public Android.os.IBinder Asbinder () {return this; } @Override public boolean ontransact (int code, ANDROID.OS.PARCEL data, android.os.Parcel reply, int flags) Throws android.os.RemoteException {switch (code) {case Interface_transaction: { Reply.writestring (descriptor); return true; } case Transaction_getpid: {data.enforceinterface (descriptor); int _result = This.getpid (); Reply.writenoexception (); Reply.writeint (_result); return true; } case Transaction_getservicename: {data.enforceinterface (descriptor); Java.lang.String _result = This.getservicename (); Reply.writenoexception (); Reply.writestring (_result); return true; } case Transaction_makesingingservice: {data.enforceinterface (descriptor); This.makesingingservice (); Reply.writenoexception (); return true; }} return Super.ontransact (code, data, reply, flags); } private static class Proxy implements Com.example.shanWu.interprocesscomm.IMyAidlInterface {private Android.os.IBinder mremote; Proxy (Android.os.IBinder remote) {mremote = remote; } @Override Public Android.os.IBinder Asbinder () {return mremote; } public java.lang.String Getinterfacedescriptor () {return descriptor; } @Override public int getpid () throws Android.os.RemoteException {Android.os.Parcel _data = Android.os.Parcel.obtain (); Android.os.Parcel _reply = Android.os.Parcel.obtain (); int _result; try {_data.writeinterfacetoken (descriptor); Mremote.transact (Stub.transaction_getpid, _data, _reply, 0); _reply.readexception (); _result = _reply.readint (); } finally {_reply.recycle (); _data.recycLe (); } return _result; } @Override Public java.lang.String getservicename () throws Android.os.RemoteException { Android.os.Parcel _data = Android.os.Parcel.obtain (); Android.os.Parcel _reply = Android.os.Parcel.obtain (); Java.lang.String _result; try {_data.writeinterfacetoken (descriptor); Mremote.transact (Stub.transaction_getservicename, _data, _reply, 0); _reply.readexception (); _result = _reply.readstring (); } finally {_reply.recycle (); _data.recycle (); } return _result; } @Override public void Makesingingservice () throws Android.os.RemoteException {Android Oid.os.Parcel _data = Android.os.Parcel.obtain (); Android.os.Parcel _reply = Android.os.Parcel.obtain (); try {_data.writeinterfacetoken (descriptor); Mremote.transact (Stub.transaction_makesingingservice, _data, _reply, 0); _reply.readexception (); } finally {_reply.recycle (); _data.recycle (); }}} static final int transaction_getpid = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int transaction_getservicename = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 1); static final int transaction_makesingingservice = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 2); } public int Getpid () throws android.os.RemoteException; Public java.lang.String Getservicename () throws android.os.RemoteException; public void Makesingingservice () throws android.os.RemoteException;}
The Stub also defines some helper methods, which pay particular attention to asinterface (), which takes a IBinder as a parameter (usually onserviceconnected () passed into the client callback method) and returns a stub interface object.
Second, realize the interface
We need to implement the interface generated by Aidl, as shown in the following example:
Private final Imyaidlinterface.stub Mbinder = new Imyaidlinterface.stub () { @Override public int Getpid () throws remoteexception { return android.os.Process.myPid (); } @Override public String Getservicename () throws RemoteException { return "Worker service~"; } @Override public void Makesingingservice () { log.d ("Guang", "singing~~~in Log ..."); Toast.maketext (Workerservice.this, "singing~~~", Toast.length_long). Show (); Not gonna work for the exception Mhandler.sendemptymessage (msg_start_sing); } ;
Now Mbinder is an instance of a Stub class and implements the interface. Next, this instance will be exposed to the client so that they can interact with the Service. When implementing the Aidl interface, we need to pay attention to the following points:
1. Because the command is not necessarily executed on the main thread, you must consider multithreading, which is thread-safe.
2. RPC commands are typically synchronous by default. If the Service spends some time hishinuma a request, it should not call from the main thread, or it will produce ANR.
3. No exception will be passed back to the caller (caller)
Third, exposing the interface to the client to use
When the client calls Bindservice () to establish a link, the onserviceconntected callback receives the Mbinder instance returned by the Service Onbind (). if the guest is in a different application, then the client should also have a copy of the Aidl file in the src/path . When the client receives IBinder in the onserviceconnected () callback, we must call the Aidl interface class. Stub.asinterface (Service), and turn its return value to our Aidl interface class name, as follows:
public class Mainactivity extends Activity implements View.onclicklistener { private serviceconnection Mserviceconn ; Private Imyaidlinterface Mservice; Private Boolean misbind = false; private int mworkerid =-1;
private void InitData () { mserviceconn = new Serviceconnection () { @Override public void onserviceconnected (ComponentName componentname, IBinder IBinder) { Mservice = IMyAidlInterface.Stub.asInterface (ibinder); LOG.D ("Guang", "onserviceconnected"); Misbind = true; } @Override public void onservicedisconnected (ComponentName componentname) { log.d ("Guang", " Onservicedisconnected "); Misbind = false; } }; }
Then you can call the service on the server from the client.
Case r.id.btn_sing: if (misbind) { try { Mworkerid = Mservice.getpid (); Toast.maketext (mainactivity.this, Mworkerid + "", Toast.length_long). Show (); Mservice.makesingingservice (); } catch (RemoteException e) { log.e ("Guang", "Service is dead" + E); } } Break
Complete Example:
Https://github.com/shanwu/InterProcessCommPractice/tree/ipc_aidl_example
Android Bound Service (ii)-----Using aidl