To achieve cross-process communication (interprocess communication referred to as IPC), Android provides a aidl Service.
Aidl is an IDL language used to generate code that can communicate between two processes on an Android device
If the traditional Chinese medicine in one process invokes the operation of an object in another process, you can use Aidl to generate serializable parameters.
Aidl is interface-oriented
Unlike binding this service, the Onbind method of the local service directly passes the IBinder object itself to the client serviceconnection
The second parameter of the Onserviceconnected method. However, the Onbind method of the remote service simply passes the proxy for the IBinder object to the client.
Create Aidl:
New-File Xxx.aidl
Xxx.aidl just defines an interface with syntax similar to Java syntax but with several differences:
The source code for the 1.AIDL definition interface must end with. aidl
The type of data used in 2.AIDL, in addition to the basic type, string,list map,charsequence,
All other types require a guide package.
Package Com.xxx.xxx;interface ICat {String getColor ();d ouble getweight ();}
Exposing the interface to the client:
Once the Aidl interface is defined, the ADT tool automatically generates an Icat.java interface in the Engineering Gen directory, which
Contains a stub inner class that implements the IBinder, icat two interfaces for the inner class, which will be used as a
The callback class for the remote service----It implements the IBinder interface, so it can be used as the return value of the service's Onbind method.
Defines a service implementation class in which the IBinder object returned by the service's Onbind method should be the icat.stub generated by ADT
instance of the subclass.
Service-Side code:
public class Aidlservice extends Service{private catbinder catbinder; Timer timer = new timer (); string[] colors = new string[]{"Red", "yellow", "BLACK"};d ouble[] weights = new double[]{2.3,3.1,1.58};p rivate String color; Private double weight;//inherits stubs, that is, implements the ICat interface, and implements the IBinder interface public class Catbinder extends stub{@Overridepublic String GetColor () throws Remoteexception{return color;} @Overridepublic Double Getweight () throws Remoteexception{return weight;} @Overridepublic void OnCreate () {super.oncreate (); catbinder = new Catbinder (); Timer.schedule (new TimerTask () {@ overridepublic void Run () {//randomly changes the value of the color, weight property within the service component. int rand = (int) (Math.random () * 3); color = Colors[rand];weight = Weights[rand]; System.out.println ("--------" + Rand);}} , 0, 800);} @Overridepublic ibinder onbind (Intent arg0) {/* return Catbinder Object * In the case of binding the ground service, the Catbinder object will be directly * The second parameter to the Onserviceconnected method of the Serviceconnection object * passed to the client; * In the case of a remote service binding, only the proxy of the Catbinder object * The second argument to the Onserviceconnected method of the Serviceconnection object * passed to the client; */return Catbinder; ①} @Overridepublic void OnDestroy () {timer.cancel ();}}
The Catbinder class inherits the Icat.stub class, which is the implementation of the ICat interface and the IBinder interface, so the program overrides the Onbind method when it returns the Catbinder
Instances of
Client Access:
It is important to note that not only the server needs Aidl interface, but also the client needs this interface, and is identical.
As a result of reading the time did not look carefully, the results tangled for two hours ...
Client code:
public class Aidlclient extends Activity{private ICat catservice;private Button get; EditText color, weight;private serviceconnection conn = new Serviceconnection () {@Overridepublic void onserviceconnected (componentname name, IBinder service) {//Gets the proxy of the object returned by the Onbind method of the remote service Catservice = ICat.Stub.asInterface (service);} @Overridepublic void onservicedisconnected (componentname name) {catservice = null;}}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); get = (Button) Findviewbyid (r.id.get); color = (EditText) Findviewbyid (r.id.color); weight = ( EditText) Findviewbyid (r.id.weight);//create the required binding service intentintent Intent = new Intent (); Intent.setaction (" Com.example.aidlservice.AIDL_SERVICE ");//Bind remote Servicebindservice (intent, Conn, service.bind_auto_create); Get.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {try{//acquired, and displays the status of the remote service Color.settext (Catservice.getcolor ()); Weight.settext (caTservice.getweight () + "");} catch (RemoteException e) {e.printstacktrace ();}}});} @Overridepublic void OnDestroy () {Super.ondestroy ();//Unbind This.unbindservice (conn);}}
Abdroid------using aidl service for cross-process call service