The main role of Aidl in Android development is to communicate across processes, and when it comes to processes, many people are very familiar with it, but why is there a concept of cross-process communication? Originally in the Android system, there is such a security mechanism, in order to each APK data independence, security, they are not directly to each other data access. So in order to achieve the data, methods, code reuse between the multiple apk, we usually adopt the method is to define the Aidl interface, so as to protect the logic of the existing code, but also can take into account the good encapsulation, each team only need to communicate good Aidl interface definition can be.
Let's go straight to the subject, where the common code logic is usually packaged separately into a standalone apk, which we might want to be a server server, in the AIDL definition. When the server break logic is defined, it can be called by other third-party code, this third-party apk we might as well be a client. However, both the server and client must ensure that the Aidl file name is the same, and that it is under the same package name. Only by satisfying these two points can we use Aidl to communicate.
If there is a common computing method, it needs to be called in multiple third-party APK. So define two Android project, a server-side program Aidlserver, a client test program testclient. Where the server, the client's Aidl interface files are located in the package "com.example.aidl", the definition of the Ical.aidl file, the code is as follows:
Package Com.example.aidl;interface ical{ double docal (double x,double y);}
When defining writing Aidl file code, the syntax rules are consistent with Java, so remember to not forget to introduce a package or write a semicolon. When the Aidl file is defined and there is no error, press and hold the Ctrl+s key to save the compilation, you will find that the project's Gen folder will automatically generate a Java file with the same name. If the Java file is correctly generated in this step, then the server side, the client's Aidl interface section has been defined, then how to expose the server interface to the client call it? The answer is through the service, let's first look at the service side of the Calservice code:
package Com.example.service;import Com.example.aidl.ical;import Android.app.service;import Android.content.intent;import Android.os.ibinder;import android.os.RemoteException; Import Android.util.log;public class Calservice extends Service {private static final String TAG = CalService.class.getNam E (); @Overridepublic ibinder Onbind (Intent Intent) {return mbinder;} @Overridepublic void OnCreate () {LOG.D (TAG, "onCreate action"); Super.oncreate ();} @Overridepublic void OnStart (Intent Intent, int startid) {log.d (TAG, "OnStart action"), Super.onstart (Intent, Startid);} @Overridepublic void OnDestroy () {LOG.D (TAG, "OnDestroy"); Super.ondestroy ();} @Overridepublic boolean onunbind (Intent Intent) {log.d (TAG, "Onunbind"); return Super.onunbind (Intent);} Private final ical.stub mbinder=new ical.stub () {@Overridepublic double docal (double x, double y) throws RemoteException { Calutils calutils=new calutils ();d ouble result=calutils.add (x, y); return result;}};}
Through Calservice can be very good to expose the Calutils common class inside the calculation method, Calutils code is as follows:
Package Com.example.service;public class calutils{public double Add (double x,double y) { return x+y; }}
Finally, you need to register the Calservice in the Androidmanifest.xml file, the code is as follows:
<service android:name= "Com.example.service.CalService" > <intent-filter> <action Android:name= "Com.example.service.CalService"/> </intent-filter></service>
Here, the server-side code logic is over, the following begins to write the test code of the client. First define the good one calculation XML page, which placed two edittext, a textview, a button, a textview, this block of XML code is not released, later in the demo. Mainly to see the activity code in the background, as follows:
Package Com.example.mytestpro;import Com.example.aidl.ical;import Android.app.activity;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.graphics.color;import Android.os.bundle;import Android.os.ibinder;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class CalActivity Extends Activity implements Onclicklistener {private static final String tag=calactivity.class.getname ();p rivate EditText etx,ety;private Button btncal;private TextView tvinfo;private ICal mservice;private serviceconnection Mserviceconnection=new serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {LOG.D ( TAG, "Do disconnected action"); mservice=null;} @Overridepublic void onserviceconnected (componentname name, IBinder service) {LOG.D (TAG, "Do ConnectedAction "); Mservice=ical.stub.asinterface (service);}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_calctivity); Initview (); Setclick (); Startbindserviceaction ();} private void Startbindserviceaction () {bundle args=new bundle (); Intent intent=new Intent (" Com.example.service.CalService "); Intent.setpackage (" Com.example.aidlserver "); Intent.putextras (args); Bindservice (Intent, mserviceconnection, context.bind_auto_create);} private void Initview () {etx= (EditText) Findviewbyid (R.ID.ETX); ety= (EditText) Findviewbyid (r.id.ety); Btncal= (Button ) Findviewbyid (r.id.btncal); tvinfo= (TextView) Findviewbyid (r.id.tvinfo);} private void Setclick () {Btncal.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btncal:excutecalaction (); break;default:break;}} private void Excutecalaction () {try {double x=double.parsedouble (Etx.gettext (). toString ());d ouble y= Double.parsedouble (Ety.gettext (). toString ()); String result= "Result:" +mservice.docal (x, y); Tvinfo.settext (result); Tvinfo.settextcolor (color.red);} catch (Exception e) {log.d (TAG, E.getmessage ());}}}
Interested readers, you can click the download server demo, client demo.
The aidl of the Android development series