[0] types of packages supported by aidl by default are Java basic types (INT, long, Boolean, etc.) and (string, list, MAP, charsequence ), how can I implement a custom type?
To pass a custom type, first make the custom type support the parcelable protocol. The implementation steps are as follows:
1> the parcelable interface must be implemented for the custom type and the Public void of the parcelable interface must be implemented.
Writetoparcel (parceldest,
Intflags) method.
2> the custom type must contain a static member named creator. The member object must implement the parcelable. Creator interface and its method.
3> Create an aidl file to declare your custom type.
Role of the parcelable interface: an instance that implements the parcelable interface can write its own state information (the State information usually refers to the value of each member variable) into parcel, you can also restore the status from parcel. Parcel is used to complete data serialization and transmission.
[1] implementation process for passing custom types between processes
1> create a custom type and implement the parcelable interface to support the parcelable protocol. For example, create person. Java in COM. Hoo. domin:
- Public class person implements parcelable
- Private integer ID;
- Private string name;
- Public Person (){}
- Public Person (integer ID, string name ){
- This. ID = ID;
- This. Name = Name;
- }
- Public integer GETID (){
- Return ID;
- }
- Public void setid (integer ID ){
- This. ID = ID;
- }
- Public String getname (){
- Return name;
- }
- Public void setname (string name ){
- This. Name = Name;
- }
- @ Override
- Public int describecontents (){
- Return 0;
- }
- @ Override
- Public void writetoparcel (parcel DEST, int flags) {// write data in javanbean to parcel
- DeST. writeint (this. ID );
- DeST. writestring (this. Name );
- }
- // Add a static member named Creator, which implements the parcelable. Creator interface.
- Public static final parcelable. creator <person> creator = new parcelable. creator <person> (){
- @ Override
- Public Person createfromparcel (parcel source) {// reads data from parcel and returns the person object
- Return new person (source. readint (), source. readstring ());
- }
- @ Override
- Public person [] newarray (INT size ){
- Return new person [size];
- }
- };
- }
2> Create an aidl file under the custom type package to declare the custom type. The file name is the same as the custom type.
Package com. Hoo. domin
Parcelable person;
3> to use a custom type in the interface aidl file, you must use import to explicitly import the data. In this example, the ipersonservice. aidl file is created under the com. Hoo. aidl package. The content is as follows:
- Package com. Hoo. aidl;
- Import CN. itcast. domain. person;
- Interface ipersonservice {
- Void save (in person );
- }
4> implement the aidl file generation interface (ipersonservice in this example), but not directly implement the interface, but implement it through the stub of the inherited interface (the aidl interface is implemented inside the stub abstract class ), and the code that implements the interface method. The content is as follows:
- Public class servicebinder extends ipersonservice. Stub {
- @ Override
- Public void save (person) throws RemoteException {
- Log. I ("personservice", person. GETID () + "=" + person. getname ());
- }
- }
5> Create a service. In the onbind (intentintent) method of the service, return the object that implements the aidl interface (in this example, servicebinder ). The content is as follows:
- Public class personservice extends Service {
- Private servicebinder = new servicebinder ();
- @ Override
- Public ibinder onbind (intent ){
- Return servicebinder;
- }
- Public class servicebinder extends ipersonservice. Stub {
- @ Override
- Public void save (person) throws RemoteException {
- Log. I ("personservice", person. GETID () + "=" + person. getname ());
- }
- }
- }
Other applications can access the service by implicit intent, and the intent actions can be customized. The androidmanifest. xml configuration code is as follows:
- <Service android: Name = ". personservice">
- <Intent-filter>
- <Action Android: Name = "com. Hoo. process. aidl. personservice"/>
- </Intent-filter>
- </Service>
6> copy the aidl file in the application and the package to the src directory of the client application. Eclipse automatically generates ipersonservice for the aidl file in the gen directory of the client application. the Java interface file, and then copy the custom type file, the type declaration aidl file, and the package to the src directory of the client application.
Finally, you can communicate with the remote service in the client application. The Code is as follows:
- Public class clientactivity extends activity {
- Private ipersonservice personservice;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- This. bindservice (new intent ("cn. itcast. process. aidl. personservice"), this. serviceconnection, bind_auto_create); // bind to the service
- }
- @ Override
- Protected void ondestroy (){
- Super. ondestroy ();
- This. unbindservice (serviceconnection); // remove the service
- }
- Private serviceconnection = new serviceconnection (){
- @ Override
- Public void onserviceconnected (componentname name, ibinder Service ){
- Personservice = ipersonservice. stub. asinterface (service );
- Try {
- Personservice. Save (new person (56, "liming "));
- } Catch (RemoteException e ){
- Log. E ("clientactivity", E. tostring ());
- }
- }
- @ Override
- Public void onservicedisconnected (componentname name ){
- Personservice = NULL;
- }
- };
- }