What is Aidl
There is no sharing of memory between processes in the Android system, so there are mechanisms that need to be provided for data communication between different processes.
To enable other applications to access the services provided by the application, the Android system is implemented using remote procedure calls (Procedure Call,rpc).
Like many other RPC-based solutions, Android uses an interface definition language (Interface definition Language,idl) to expose the interfaces of the service.
Three of Android's four components (Activity, Broadcastreceiver, and ContentProvider) can be accessed across processes, and another Android application component service can do the same. As a result, this service that can be accessed across processes is referred to as the Aidl (Android Interface Definition Language) service.
Implement Aidl
Establish Aidlserver and aidlclient projects, such as:
①aidlserver
Create a new Icalculate interface and modify the file suffix to. aidl
1 Package com.example.calculate; 2 3 Interface icalculate {4 double docalculate (doubledouble b); 5 }
. aidl generates Icalculate.java interface files in the "gen/Package name/" Directory after compilation
Create a new Calculateservice service with Mybinder in the service that implements the Icalculate interface
1 Public classCalculateserviceextendsService {2 3 PrivateMybinder Mbinder; 4 5 @Override6 Public voidonCreate () {7 //TODO auto-generated Method Stub8 Super. OnCreate ();9 TenMbinder =NewMybinder (); One A } - - the @Override - Public voidOnDestroy () { - //TODO auto-generated Method Stub - Super. OnDestroy (); + } - + A @Override at Publicibinder onbind (Intent arg0) { - //TODO auto-generated Method Stub - returnMbinder; - } - - in Public classMybinderextendsicalculate.stub - { to + @Override - Public DoubleDocalculate (DoubleADoublebthrowsRemoteException { the //TODO auto-generated Method Stub * returnA +b; $ }Panax Notoginseng - } the +}
Declaring service in Androidmanifest.xml
1 <Service2 Android:name= "Com.example.aidlserver.CalculateService"3 android:enabled= "true" >4 <Intent-filter>5 <ActionAndroid:name= "Com.example.calculate.ICalculate" />6 </Intent-filter>7 </Service>
②aidlclient
Copy the Com.example.calculate package in Aidlserver into the aidlclient (must be the same package across process calls)
Use the Bindservice method to invoke the Calculateservice service
1 icalculate Mbinder;2 3Serviceconnection mcon=Newserviceconnection () {4 5 @Override6 Public voidonservicedisconnected (componentname arg0) {7 //TODO auto-generated Method Stub8 9 }Ten One @Override A Public voidonserviceconnected (componentname arg0, IBinder arg1) { -Mbinder=ICalculate.Stub.asInterface (arg1); - the - - } -};
1 Intent intent=new Intent ("Com.example.calculate.ICalculate");
Bindservice (Intent, MCon, context.bind_auto_create);
The Mainactivity.java code is as follows:
1 Public classMainactivityextendsactionbaractivity {2 3 4 Button Btnplus;5 EditText Et1;6 EditText Et2;7 TextView Tvresult;8 9 Ten icalculate Mbinder; One AServiceconnection mcon=Newserviceconnection () { - - @Override the Public voidonservicedisconnected (componentname arg0) { - //TODO auto-generated Method Stub - - } + - @Override + Public voidonserviceconnected (componentname arg0, IBinder arg1) { AMbinder=ICalculate.Stub.asInterface (arg1); at - - - } - }; - in - @Override to protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the * $btnplus=(Button) Findviewbyid (r.id.btnplus);Panax Notoginsenget1=(EditText) Findviewbyid (R.ID.ET1); -Et2=(EditText) Findviewbyid (R.id.et2); thetvresult=(TextView) Findviewbyid (r.id.tvresult); + ABtnplus.setonclicklistener (NewOnclicklistener () { the + @Override - Public voidOnClick (View arg0) { $ $ if(mbinder==NULL) - { - return; the } - WuyiString num1=Et1.gettext (). toString (); theString num2=Et2.gettext (). toString (); - Wu if(Textutils.isempty (NUM1) | |Textutils.isempty (num2)) - { About return; $ } - - Doubled1=double.parsedouble (NUM1); - DoubleD2=double.parsedouble (num2); A + DoubleTotal=-1; the -LOG.I ("Mainactivity", "OnClick () d1=" +d1+ "D2" +D2); $ the the Try { theTotal=mbinder.docalculate (D1, D2); the}Catch(RemoteException e) { - //TODO auto-generated Catch block in e.printstacktrace (); the } the About theTvresult.settext ("Result:" +Total ); the the + } - }); the BayiIntent intent=NewIntent ("Com.example.calculate.ICalculate"); the the Bindservice (Intent, MCon, context.bind_auto_create); - - } the the the the @Override - protected voidOnDestroy () { the //TODO auto-generated Method Stub the Super. OnDestroy (); the Unbindservice (mCon);94 } the the the 98 About}
Note: After Bindservice, remember Unbindservice
Cross-process call technology in Android Aidl