Invoking Service (aidl service) across processes
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.
In the previous article ( about the four components (service opening and closing) in Android), we described how developers can customize their services, but these
The service cannot be accessed by other applications, in order to make other applications also have access to the services provided by the application, and the Android system uses
Remote Procedure Call (Procedure call,rpc) mode. Like many other RPC-based solutions, Android uses a
The interface Definition language (Interface definition Language,idl) exposes the interfaces of the service, so that this cross-process-access service can be called
(Android Interface Definition Language) Aidl Services.
steps to establish a AIDL service
The establishment of AIDL service is more complicated than ordinary service, the concrete steps are as follows.
(1) Create a file with the extension aidl in the Java source file directory of the Eclipse Android project, which is similar to the Java generation
Code, but it will be slightly different.
(2) If the contents of the Aidl file are correct, ADT will automatically generate a Java interface file (*.java).
(3) Set up a service class (subclass of Services).
(4) Implement the Java interface generated by the Aidl file.
(5) Configure the Aidl service in the Androidmanifest.xml file, noting that the attribute value Android:name in the <action> tag is the client
The ID of the service to reference, which is the parameter value of the intent class construction method.
Establish aidl service
The following program creates a simple Aidl service on the server that has two get methods to get the name and age, and the steps to create this AIDL service
As follows.
(1) Create a aidl file, such as the Iperson.aidl file in.
The contents of the Iperson.aidl file are as follows:
<pre name= "code" class= "HTML" >package com.aidl;interface iperson{string getName (); int getage ();}
The contents of the Iperson.aidl file are very similar to Java code, but note that you cannot add modifiers, such as public, private, and Aidl
Data types such as InputStream, OutputStream, etc. that are not supported by the service.
(2) If the contents of the Iperson.adil file are correct, ADT will automatically generate a Iperson.java file.
(3) Write a MyService class that inherits from the service and defines an inline class Personimpl in the MyService class that inherits from
The code for the Iperson.stub,myservice class is as follows:
public class MyService extends Service{public class Personimpl extends iperson.stub{@Overridepublic String getName () thro WS RemoteException {return "Bill";} @Overridepublic int Getage () throws remoteexception {return 25;}} @Overridepublic ibinder onbind (Intent Intent) {return new Personimpl ();}}
Note: the Onbind () method above must return the Personimpl object, otherwise the client cannot obtain the service object.
(4) Configure the MyService class in the Androidmanifest.xml file with the following code:
<service android:name= "Com.example.aidlserviceproject.service.MyService" > <intent-filter > <action android:name= "Com.aidl.IPerson"/> </intent-filter></service>
Note: The "Com.aidl.IPerson" is the ID that the client uses to access the AIDL service.
At this point, the service-side Aidl service is written, then the client code is written, the new project, the service side of the auto-generated
The Iperson.java file is copied to the client project along with the package directory, such as:
The following program implements the binding Aidl service and obtains server-side data:
public class Mainactivity extends Activity implements onclicklistener{private button Btn_aidl;private button btn_get; Private TextView tv_show;private Intent mipitent;private iperson mipiperson=null;private serviceconnection conn=new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {} @Overridepublic void onserviceconnected (componentname name, IBinder service) {mipiperson=iperson.stub.asinterface (service); Btn_ Get.setenabled (True);}}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); Initview (); Initevent (); } private void InitData () {mipitent=new Intent ("Com.aidl.IPerson"); } private void Initview () {btn_aidl= (Button) This.findviewbyid (R.ID.BTN_AIDL); btn_get= (Button) This.findviewbyid (r.id.btn_get); tv_show= (TextView) This.findviewbyid (r.id.tv_show); Btn_get.setenabled (FALSE); } private void Initevent () {Btn_aidl.setonclicklistener (this); Btn_get.setonclicklistener (this); } @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.btn_aidl://binding Aidl service Bindservice (mipitent, Conn, Co ntext. bind_auto_create); break;case r.id.btn_get://Get service-side data try {tv_show.settext ("Name:" +mipiperson.getname () + "\ n Age:" + Mipiperson.getage ());} catch (RemoteException e) {e.printstacktrace ();} Default:break;}}}
The code above uses the Bindservice method to bind the Aidl service, where the ID of the AIDL service needs to be specified, that is, in the <action> tag
The Android:name property value. A Serviceconnection object is required at bind time, and when the binding succeeds, the system calls
The serviceconnection.onserviceconnected method obtains the Aidl service object by using the service parameter of this method. finally run the service end first
And then run the client program.
The effect is as follows:
Aidl Services for delivering complex data
AIDL Services only support a limited number of data types, so if you pass some complex data with the AIDL service, you need to take a step further, Aidl service support
The following data types are held.
(1) Java Simple Type (int, char, Boolean, etc.), do not need to import.
(2) string and charsequence, no import required.
(3) The element types of list and map,list and map objects must be the data types supported by the AIDL service and do not need to be imported.
(4) Aidl automatically generated interface, need to import.
(5) The class that implements the Android.os.Parcelable interface needs to be imported.
The data types passed by the following programs are the person class, and the server code is as follows:
(1) New person implementation Parcelable interface
public class person implements parcelable{private String name;private int age;public static final parcelable.creator<p Erson> creator=new creator<person> () {@Overridepublic person[] newarray (int size) {return new person[size];} @Overridepublic person Createfromparcel (Parcel source) {return new person (Source.readint (), source.readstring ()) ;}}; Public person () {}public person (int age,string name) {this.name=name;this.age=age;} @Overridepublic int describecontents () {return 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {Dest.writeint (age);d est.writestring (name);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
The person implements the Android.os.Parcelable interface, which is used to serialize the object, and must have a static constant in the person class, constant
The name must be Creator, and the data type of the Creator constant must be parcelable.creator. In the Writetoparcel method, you need to set the sequence
Parcel objects, Note that the order of the reads must be consistent with the order in which they are written.
(2) Then create a iperson.aidl file with the following code:
Package Com.aidl;import com.aidl.person;interface Iperson{person Getperson ();}
(3) Create a Person.aidl file with the following code:
Package com.aidl; Parcelable person;
(4) Create a MyService class with the following code:
public class MyService extends Service {public class Personimpl extends Iperson.stub {@Overridepublic person Getperson () t Hrows remoteexception {Person person = new person ();p erson.setname ("Bill");p erson.setage, return person;}} @Overridepublic ibinder onbind (Intent Intent) {return new Personimpl ();}}
The project directory structure of the end server is as follows:
OK, the service end of the Aidl service is written, and then write the client program, the server Person.java and Iperson.aidl together with the package
To client engineering, such as:
The following code implements the data in the person object that gets the service side:
public class Mainactivity extends Activity implements onclicklistener{private button Btn_aidl;private button btn_get; Private TextView tv_show;private Intent mipitent;private iperson mipiperson=null;private serviceconnection conn=new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {} @Overridepublic void onserviceconnected (componentname name, IBinder service) {mipiperson=iperson.stub.asinterface (service); Btn_ Get.setenabled (True);}}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); Initview (); Initevent (); } private void InitData () {mipitent=new Intent ("Com.bill.aidl.IPerson"); } private void Initview () {btn_aidl= (Button) This.findviewbyid (R.ID.BTN_AIDL); btn_get= (Button) This.findviewbyid (r.id.btn_get); tv_show= (TextView) This.findviewbyid (r.id.tv_show); Btn_get.setenabled (FALSE); } private void Initevent () {Btn_aidl.setonclicklistener (this); Btn_get.setonclicklistener (this); } @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.btn_aidl://binding Aidl service Bindservice (mipitent, Conn, Co ntext. bind_auto_create); Break;case r.id.btn_get://gets the service-side data try {person Person=mipiperson.getperson (); Tv_show.settext ("Name: "+person.getname () +" \ n Age: "+person.getage ()); catch (RemoteException e) {e.printstacktrace ();} Default:break;}}}
Run the server-side program first, run the client program, the effect is as follows:
--------------------------------------------------------------------------------------------------------------- ----------------------------------------
Reprint Please specify the Source: http://blog.csdn.net/hai_qing_xu_kong/article/details/47748779 Emotional Control
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
About the four components in Android (use of the Aidl service)