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