Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47071927
In the previous blog "Android--aidl Summary", we briefly introduced the use of Android Aidl, and in this blog post, we will delve into the use of Android Aidl, similarly, here we also use a small example to learn about Android The use of aidl.
Benefits: The establishment of a common service mechanism between multiple applications, through AIDL to achieve data sharing and data interoperability between different applications,
This article includes: 1, create Aidl server. 2, create the Aidl client. 3. The client invokes the service interface provided by the server.
1, create the Aidl service side.
Create a new istudentservice.aidl in the src of Android
Package Com.example.studentservice;import com.example.studentservice.Student;; Interface istudentservice{ Map getmap (in String test_class,in Student Student); Student getstudent (); }
The Student class is a serialized class, which is serialized using the Parcelable interface. The Student class code is as follows:
Package com.example.studentservice; Import Android.os.parcel;import android.os.Parcelable; public class Student implements parcelable{private int age; private String name; Public Student (Parcel source) {//TODO auto-generated constructor stub = Source.readint (); Name = Source.readstring (); } public Student () {//TODO auto-generated constructor stubs} public int getage () {return age; public void Setage (int.) {this.age = age; } public String GetName () {return name; } public void SetName (String name) {this.name = name; public static final parcelable.creator<student> Creator = new Creator<student> () { @Override public student[] NewArray (int size) {//TODO auto-generated method stub return New Student[size]; } @Override Public Student createfromparcel (Parcel source) { TODO auto-generated Method stub return new Student (source); } }; @Override public int describecontents () {//TODO auto-generated method stub return 0; } @Override public void Writetoparcel (Parcel dest, int flags) {//TODO auto-generated method Stub de St.writeint (age); Dest.writestring (name); } }
Here are three points to note:
1. There must be a static constant in the student class, the constant name must be Creator, and the data type of the Creator constant must be parcelable.creator
2. In the Writetoparcel method, the value to be serialized is required to be written to the parcel object.
3. Once you have finished writing student, you must create a new Student.aidl file that will enter the following:
Parcelable Student; The writing here is for the above-mentioned interface *.aidl files can be found, and the student class object is found through this file.
If the above steps pass smoothly, Android will automatically generate a *.java file named *.aidl file in the same directory as the R file in the Gen directory, such as:
After a successful build, let's write a Aidl service class with the following code:
Package Com.example.studentservice;import Java.util.hashmap;import Java.util.map;import android.app.Service;import Android.content.intent;import Android.os.ibinder;import android.os.RemoteException; public class Studentservice extends service{@Override public ibinder onbind (Intent Intent) {//TODO auto-g enerated method Stub return new Studentserviceimple (); } public class Studentserviceimple extends Istudentservice.stub {@Override public Student Getstudent () throws RemoteException {Student Student = new Student (); Student.setage (25); Student.setname ("Zhang san"); return student; } @Override Public Map getmap (String test_class, Student Student) throws RemoteException { TODO auto-generated Method Stub map<string, object> Map = new hashmap<string, Object> ;(); Map.put ("Class", "06109091"); Map.put ("Age", Student.getage ()); Map.put ("Name", Student.getname ()); return map; } } }
As the code above, the Studentservice service class has a subclass and inherits from the *.java file we generated above, overriding the two interface methods we declared in *.aidl, which implements its function. The above IBinder must return the subclass object of this service class, otherwise the client will not be able to obtain the service object.
Finally, the operation of the service, then you have to register the service class in the manifest file, the code is as follows:
<service android:name= ". Studentservice " android:exported=" true " android:enabled=" true " android:process=": Remote > <intent-filter> <action android:name= "Com.example.studentservice.IStudentService" ></action > </intent-filter></service>
At this point, the service side has been developed, the following development of the client terminal.
2. Create Aidl client
The same is a new project, it is important to note that the server will have a successful generation of the Gen directory under the copy of the package, put in the SRC folder of our new project, such as:
Because Iserviceservice this generated class, referenced to the student, so here will student also copied over.
At this point, the client's creation has been completed, the following we will use to create a client to invoke the service-side method.
3, the client calls the service interface provided by the server
Package com.example.studentclient; Import Com.example.studentservice.IStudentService; Import Android.os.bundle;import android.os.ibinder;import android.os.remoteexception;import android.app.Activity; Import Android.app.alertdialog;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.view.menu;import Android.view.View ; Import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends Activity {private Button btn1, btn2; Private Istudentservice stuservice = null; Private Serviceconnection serviceconnection = new Serviceconnection () {@Override public void Onser vicedisconnected (componentname name) {//TODO auto-generated method stub} @Override public void onserviceconnected (componentname name, IBinder service) {//TODO auto-generated Method stub Stuservice = IStudentService.Stub.asInterface (service); } }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); BTN1 = (Button) Findviewbyid (R.id.button1); BTN2 = (Button) Findviewbyid (R.id.button2); Btn1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { TODO auto-generated Method Stub Bindservice (new Intent ("Com.example.studentservice.IStud Entservice "), Serviceconnection, context.bind_auto_create);//; } }); Btn2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { TODO auto-generated Method stub StringBuilder sb = new StringBuilder (); try {if (Stuservice = = Null) {new Alertdialog.builder (mainactivity.this). Settitle ("Error"). setmess Age ("Stuservice is null"). Setpositivebutton (Android. R.string.ok, NULL). Show (); Return } sb.append ("Student Name:" + stuservice.getstudent (). GetName () + "\ n"); Sb.append ("Age:" + stuservice.getstudent (). Getage () + "\ n"); Sb.append ("Map object content is as follows:" + Stuservice.getmap ("China", Stuservice.getstudent ()) . toString ()); } catch (RemoteException e) {//TODO auto-generated catch block E.printstacktrace () ; } new Alertdialog.builder (Mainactivity.this). Settitle ("Call External Services"). Setmessage (Sb.tostr ING ()). Setpositivebutton (Android. R.string.ok, NULL). Show ();} }); } }
By initializing the imyservice inside the serviceconnetction, the object can be manipulated, and the object will have all of our data to be processed.
4. Summary
The use of aidl must exist both the client and the server side, that is, the client on this machine, the server is also on this machine, to use the client must be in advance to register the service on the server.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android's--aidl in depth