Android------Delivering complex data using Aidl service

Source: Internet
Author: User



The data type that the instance transmits is a custom type.


Android requires that the parameters and return values of the remote service be called to implement the Parcelable interface.


Implementing the Parcelable interface is equivalent to a custom serialization mechanism provided by Android.


Implementing the Parcelable interface requires not only the implementation of the methods defined in the interface, but also the requirement to define a named creator in the implementation class.
A static filed of type Parcelable.creator. In addition, these custom types are required to be defined using the AIDL code.



Service side:

Custom two types: person with pet, where the person object acts as a parameter to invoke the remote service, and pet will be the return value.

Both the person and the pet class must implement the Parcelable interface and define a static filed named Creator in the implementation class.


To define the person class, first aidl to define the person class:


Parcelable person;

Next define the class that the person implements Parcelable


public class Person implements parcelable {private Integer id;private string name;private string pass;public person () {SUP  ER ();//TODO auto-generated constructor stub}public person (Integer Idinteger, string name, String pass) {super (); this.id = Idinteger;this.name = Name;this.pass = Pass;} Public Integer Getidinteger () {return ID;} public void Setidinteger (Integer idinteger) {this.id = Idinteger;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getpass () {return pass;} public void SetPass (String pass) {This.pass = pass;} @Overridepublic int hashcode () {//TODO auto-generated method stubfinal int prime = 31;int result = 1;result = Prime * Res Ult + ((name = = null)? 0:name.hashcode ()); result = Prime * result + (pass = = null)? 0:pass.hashcode ()); return Resul t;} @Overridepublic boolean equals (Object o) {if (this = = O) {return true;} if (o = = null) {return false;} if (getclass () = O.getclass ()) {return false;} person other = (person) o;iF (name = = null) {if (other.name! = null) {return false;}} else if (!name.equals (Other.name)) {return false;} if (pass = = null) {if (Other.pass! = null) {return false;}} else if (!pass.equals (Other.pass)) {return false;} return true;} Implement parcelable the method must be implemented @overridepublic int describecontents () {return 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {//writes the data contained in this object to Parceldest.writeint (ID);d est.writestring ( Name);d est.writestring (pass);} public static final parcelable.creator<person> Creator = new parcelable.creator<person> () {@Overridepublic Person Createfromparcel (Parcel source) {//reads data from Parcel, returns the person object return new person (Source.readint (), Source.readstring (), source.readstring ());} @Overridepublic person[] NewArray (int size) {//TODO auto-generated method Stubreturn new Person[size];}};}


Then the pet class, which is the first to define the pet in the Aidl:




Then is the definition of pet class implementation parcelable:


public class Pet implements parcelable {private string name;private double weight;public pet (String name, double weight) { Super (); this.name = Name;this.weight = weight;} Public Pet () {super ();//TODO auto-generated constructor stub}public String getName () {return name;} public void SetName (String name) {this.name = name;} Public double Getweight () {return weight;} public void Setweight (double weight) {this.weight = weight;} @Overridepublic int describecontents () {//TODO auto-generated method Stubreturn 0;} @Overridepublic String toString () {return "Pet [name=" + name + ", weight=" + weight + "]";} @Overridepublic void Writetoparcel (Parcel dest, int flags) {dest.writestring (name);d est.writedouble (weight);} Add a static member named Creator that implements the Parcelable.creator interface public static final parcelable.creator<pet> Creator = new Parcelable.creator<pet> () {@Overridepublic pet Createfromparcel (Parcel source) {//reads data from Parcel, returns the Pet object return New Pet (Source.readstring (), source.readdouble ());} @OverridepublIC pet[] NewArray (int size) {return new pet[size];};} 


With the person and pet custom classes, the next step is to define the interface for the communication: Ipet


Or is it first defined in Aidl:


Package Com.example.complexaidlservice;import Com.example.complexaidlservice.pet;import com.example.complexaidlservice.person;//defines a person object as an incoming object interface ipet{list<pet> getpets (in the person owner);}


The next step is to develop the service class:


public class Complexservice extends Service {private Petbinder petbinder;private static Map<person, list<pet>& Gt Pets = new Hashmap<person, list<pet>> (), static {arraylist<pet> List1 = new arraylist<pet> (); List1.add (New pet ("4.3"), List1.add (New pet ("blessing", 5.4));p ets.put (New person (1, "Sun", "Sun"), List1); arraylist<pet> list2 = new arraylist<pet> () List2.add (New pet ("Kitty", 2.3)) List2.add (New pet ("garfiled") , 3.1));p ets.put (New person (2, "Bai", "Bai"), List2); @Overridepublic void OnCreate () {super.oncreate ();p etbinder = new Petbinder (); @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn Petbinder;} Inherit stubs, that is, implement the Ipet interface, and implement the IBinder interface public class Petbinder extends Stub {@Overridepublic list<pet> getpets ( Person owner) throws RemoteException {//TODO auto-generated method Stubreturn Pets.get (owner);}} @Overridepublic void OnDestroy () {}}




The next step is to write the client code:

Remember that in the previous article, it is necessary to copy the Aidl interface of the server, transfer complex data, not only to copy the Ipet.
Also copy the Person.java,person.aidl Pet.aidl,pet.java to the client.

or by the way the remote service was previously bound, and in the Onserviceconnected method of the Serviceconnection implementation class
Gets the proxy object returned by the Onbind method of the remote service.



public class Complexclient extends Activity {private Ipet petservice;private Button get; EditText Personview; ListView ShowView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);p Ersonview = (EditText) Findviewbyid (R.id.person); ShowView = (ListView) Findviewbyid (r.id.show); get = (Button) Findviewbyid (r.id.get); Intent Intent = new Intent (); Intent.setaction (" Com.example.complexaidlservice.COMPLEXSERVICE ");//Bind remote Servicebindservice (intent, Conn, service.bind_auto_create) ; Get.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {try {String personname = Personview . GetText (). toString ();//method to invoke remote service list<pet> pets = petservice.getpets (new person (1,personname, personname ) or//The list returned by the program is packaged as arrayadapterpterarrayadapter<pet> adapter = new Arrayadapter<> (Complexclient.this, Android. R.layout.simple_list_item_1, pets); Showview.setadapter (adapter);} catch (remoteexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}});} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); This.unbindservice (conn);} 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 Petservice = IPet.Stub.asInterface (service);} @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated method stubpetservice = null;}};}










Android------Delivering complex data using Aidl service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.