What is Aidl?
Aidl (Android Interface definition Language) is an Android interface definition language for interprocess communication (IPC) that enables cross-process communication between multiple application components and a service. This enables multiple applications to share the functionality of a service.
Second, why the IPC communication through the Aidl?
The system assigns a process to an application, the process runs in its own memory space, a process cannot directly access another process, and if it wants to communicate between processes, the AIDL,AIDL is the code that generates the communication between the two processes on the Android device.
Third, demo example
We need to create two apps, representing two processes.
1, the service side write a iaidl.aidl file,
Package Com.txfy.aidl;interface iaidl{void testaidl ();}
2. Create a remote service
public class Aidlaservice extends Service {private static final String TAG = "Aidlservice";p rivate void Log (String str) {L OG.I (TAG, "----------" + str + "----------");} public void OnCreate () {Log ("service created");} public void OnStart (Intent Intent, int startid) {Log ("service Started id =" + startid);} The method that is called when binding is public IBinder Onbind (Intent t) {Log ("service on Bind");//Returns the object to the client's Onserviceconnectedreturn new Mybinder ( );} public void OnDestroy () {Log ("Service on Destroy"); Super.ondestroy ();} Unbind service Public, Boolean onunbind (Intent Intent) {Log ("service on Unbind"); return Super.onunbind (Intent);} public void Onrebind (Intent Intent) {Log ("service on Rebind"); Super.onrebind (Intent);} Create binder, return to client's private class Mybinder extends Stub {//Client calls Testaidl () through proxy object, Testaidl () then call the method inside the service @overridepublic void Testaidl () throws RemoteException {callMe ();}} public void CallMe () {Log (' Call service Me ');}}
3, androidmanifest.xml configuration, note the service to be placed on the folder of the application unique package name
<service android:name= "Com.example.AliPay.AidlAService" > <intent-filter> <action android: Name= "Com.txfy.testAidl" ><!--This is an implicit intent, the name is arbitrary-- </action> </intent-filter> < /service>
Application structure
4. Copy iaidl.aidl file to the client,the package name of the Iaidl.aidl must match the package name on the server side
5. Client Aidlaactivity.java code calls remote service
public class Aaidlactivity extends Activity {private iaidl mitestaidl;private button maidlcallbackaidl;private button mAi Dlunbindaidl;private serviceconnection mconnection; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_aidl);//Initialize service Initservice (); InitView (); Setonclicklistener ();} private void Initservice () {Intent service = new Intent ();//implicit invocation of remote service//<service android:name= " Com.example.AliPay.AidlAService ">//<intent-filter>//<action android:name=" Com.txfy.testAidl " >//</action>//</intent-filter>//</service>service.setaction ("Com.txfy.testAidl"); mConnect Ion = new Myserviceconnection (); Bindservice (service, mconnection, context.bind_auto_create);} private void Setonclicklistener () {Maidlcallbackaidl.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {try {//Call Remote service Mitestaidl.testaidl ();} catch (RemoteException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}); Maidlunbindaidl.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Unbindservice ( mconnection);}});} Private class Myserviceconnection implements Serviceconnection {@Overridepublic void onserviceconnected (componentname Name, IBinder service) {//IService = IService.Stub.asInterface (service);//Return his proxy class mitestaidl = IAidl.Stub.asInterface ( Service);} @Overridepublic void onservicedisconnected (componentname name) {}}private void Initview () {maidlcallbackaidl = (Button) Findviewbyid (r.id.id_aidlcallback_btn); maidlunbindaidl = (Button) Findviewbyid (R.ID.ID_AIDLUNBIND_BTN);}}
Special NOTE: If an error occurs
Enforceinterface () expected ' com.txfy.aidl.IAidl ' but read ' com.example.AliPay.IAidl '
That's why you didn't put Iaidl.aidl on the server. Under the unique package name, the client defaults to the service side
Enforceinterface () expected ' com.txfy.aidl.IAidl ' but read ' com.example.AliPay.IAidl '