0. Preface
either android IPC mechanism, so-called IPC ( inter-process communication ) that is interprocess communication threads and processes are very different concepts thread is CPU The smallest cell that is called, the process is generally in pc and mobile devices refer to a program or an application where a process can contain multiple threads.
IPC Way There are many, in Android commonly used in the IPC methods include Bundle , Documents, Messenger , Aidl , ContentProvider and the Socket and other ways.
Android Development--Aidl of interprocess communication (I.) is described in Aidl A simple usage, this article mainly explains the use of Aidl the way the custom object is passed.
1. Serialization
Aidl The data types supported by the interface of the file definition are as follows:
Eight basic data types for 1.Java (Byte,int,short,long,float,double,char,boolean)//2.string//3.charsequence//4.list, All elements in the list must be data types supported by the Aidl file, for example, all elements in List<string>//5.map,map must be data types supported by the Aidl file,//6. Other aidl-defined interfaces to manually add import// 7. Other classes declared in the Aidl file, to add the import manually
In the remote adder in Android development-the Aidl (i) of interprocess communication , we passintThis article mainly introduces the last kind of data, otherAidlThe class declared in the file, which can be a custom class, is therefore more free to pass the combination of data types. Butcustom classes to completeIPCmust be serialized, serialization hasSerializableand theparcelabletwo methods, the latter of which isAndroid, so it's more efficient, but it's more cumbersome to use.
First look at the directory structure of the complete project:
Person.java is our custom class. The goal we want to achieve is to pass the object's content across processes. The serialization of the class is implemented first.
Package com.example.ipc_aidl_obj;/** * Java beans * Created by Seu_calvin on 2017/1/3. */import Android.os.parcel;import Android.os.parcelable;public class person implements parcelable{private String name; private int age; Public person (String name, Int. age) {this.name = name; This.age = age; }//read out the previously written data from Parcel protected person (Parcel in) {this.name = In.readstring (); This.age = In.readint (); public static final parcelable.creator<person> Creator = new parcelable.creator<person> () {//deserialization and read the data we wrote in the Writetoparcel method @Override public Person Createfromparcel (Parcel in) {return new P Erson (in); } @Override Public person[] NewArray (int size) {return new person[size]; } }; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public int Getage () {return age; } public void setage (int age) {this.age = age; }//Implement the Parcelable interface required to override method 1, generally without the tube @Override public int describecontents () {return 0; }//Implement the Parcelable interface required to override Method 2, write each field of the object to the Parcel @Override public void Writetoparcel (Parcel dest, int flags) { Dest.writestring (name); Dest.writeint (age); } @Override Public String toString () {return "person{" + "Name= '" + name + ' \ ' + ", age=" + Age + '} '; }}
of our Personclassrealized theparcelableInterfacethat overwrite the interface.describecontents (),Writetoparcel ()two methods that return the content description of the current object, and return if there is a descriptor1, the vast majority of cases are returned0. The latterwrites an object to the serialization structure. By looking at the documentation we foundParcelis notWriteshort ()This way, and that's exactly whatAidlcannot be delivered Shorttypethe reason for the data. And then it's statically creating the constructor .CREATOR, pay attention to addFinaldecorated and the name must beCREATORcannot be changed, whereCreatefromparcel ()used to write us down inParcelThe data in is read out. It is important to note which field is written first and which field is read first. This allows our custom entity classes to be serialized for future use.
2. Create a aidl file
a total of two Aidl file, one is the declaration of our custom object, and the other is the interface declaration. In the latter, explicit import of the former is required .
Ipersonn.aidlpackage com.example.ipc_aidl_obj;//Declare Any non-default types here with import statementsparcelable P erson;//igetdata.aidlpackage com.example.ipc_aidl_obj;//Declare any non-default types here with Import Statementsimport Com.example.ipc_aidl_obj. Person;interface igetdata{ list<person> getpersonlist (in person person);}
in the interface. getpersonlist () method to send the client Person object into List in and return List all of the objects, here's what needs to be said getpersonlist () method in the inch keywords, that is, the trend of data , there are In/out/inout three different types. Choose the right type of data to suit your needs.
3. implementing the interface
Package com.example.ipc_aidl_obj;/** * Service side * Created by Seu_calvin on 2017/1/3. */import Android.app.service;import Android.content.intent;import Android.os.ibinder;import Android.os.remoteexception;import Android.support.annotation.nullable;import Java.util.ArrayList;import Java.util.list;public class Iremoteservice extends Service { list<person> persons; @Nullable @Override public ibinder onbind (Intent Intent) { persons = new arraylist<> (); return ibinder; } Private IBinder IBinder = new Igetdata.stub () { @Override public list<person> getpersonlist ) throws RemoteException { persons.add (person); return persons;}} ;}
the server runs in an independent process, primarily Aidl The methods in the interface are implemented.
4. Client Calls
first, the binding of the service. And get the Binder instance returned by the server in the callback method of serviceconnection .
Intent Intent = newintent (Mainactivity.this,iremoteservice.class); Bindservice (intent,serviceconnection,bind_auto_ CREATE); Private Serviceconnection serviceconnection = Newserviceconnection () { @Override public voidonserviceconnected (componentname name, IBinder service) { mgetdata =igetdata.stub.asinterface (service); } @Override public voidonservicedisconnected (componentname name) { }};
When you click the button, you canthe remote call obtains theIgetdataobject thatgetpersonlist ()Method, an initialized person class object is passed in as a parameter, and the contents of the person instance in the returned list are all toast, click four times, add four person and its age information (Dear Dong da 114 years old, Android6 years old, I am always 18 years old Qaq), the effect is as follows, so that the entire client and servercustom objects are passed across processesthe process is done using AIDL.
Reference Source Address Click to download . Please give us more praise support ~
Android Development--Aidl of interprocess communication (II.)