"Grooming" Examples of practices using AIDL to pass complex objects across processes

Source: Internet
Author: User

First define the object class, and implement the Parcelable interface, implement several methods within the interface, see the code, Person.java

 Packagecom.example.u3.aidltest;ImportAndroid.os.Parcel;Importandroid.os.Parcelable;/*** Created by U3 on 2015/3/11.*/ Public classPersonImplementsparcelable {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getpass () {returnPass; }     Public voidSetPass (String pass) { This. pass =Pass; }    PrivateString Pass; @Override Public intdescribecontents () {return0; }      Public Static FinalCreator<person> Creator =NewCreator<person>() {@Override PublicPerson createfromparcel (Parcel source) {return NewPerson (source); } @Override PublicPerson[] NewArray (intsize) {            return NewPerson[size]; }    };//used to create     PublicPerson ()//for testing{Name= "Hel"; Pass= "213"; } @Override Public voidWritetoparcel (Parcel dest,intflags)        {dest.writestring (name);    Dest.writestring (pass); }   PrivatePerson (Parcel in)//must be the same as the previous write order, otherwise the value will be wrong{Name=in.readstring (); Pass=in.readstring (); }}

Then, create a new two aidl file, a declaration interface, a declaration of the above class, declaring the class's aidl name must be the same as the class, see the code

Person.aidl

 Package com.example.u3.aidltest;//package name parcelable person;

Myaidl.aidl

// Myaidl.aidl  Package com.example.u3.aidltest; // Declare any non-default types here with import statements Import com.example.u3.aidltest.person;//need to first introduce Interface  myaidl {    /**     * Demonstrates some basic types that's can use as parameters     * and return values in Aidl.      */ Person     Getperson (in person s);}

Then use the automatically generated class in the service, the implementation method, see Code

 Packagecom.example.u3.aidltest;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;Importandroid.os.RemoteException;ImportAndroid.util.Log;/*** Created by U3 on 2015/3/11.*/ Public classServerextendsService {classMyextendsMyaidl. Stub {//inherit automatically generated class, implementation method @Override PublicPerson Getperson (person s)throwsremoteexception {log.v ("SK", S.getname () + "" +S.getpass ()); S.setname ("Hi!!!!"); returns; }} @Override Publicibinder onbind (Intent Intent) {return NewMy (); }}

Then on the client, copy the Aidl file with the package and person as well as his package

The structure of the two sides is as follows

Service-side structure

Client structure

"Grooming" Examples of practices using AIDL to pass complex objects across processes

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.