The Android service activity interacts with remote services that pass complex data types

Source: Internet
Author: User

Remote services are often not just passing Java basic data types. There are some limitations and regulations that need to be taken into Android:

    1. Android supports string and charsequence
    2. If you need to use other Aidl interface types in Aidl, import is required, even under the same package structure;
    3. Android allows the delivery of classes that implement the Parcelable interface, requiring import;
    4. Android supports the collection interface type list and map, but there are some limitations, the element must be basic or the above three cases, do not need the Import collection interface class, but need to relate to the type import of the element;
    5. Non-basic data types, also not string and charsequence types, require directional indications, including in, out, and Inout,in are set by the client, out is set by the server, and InOut is both configurable.

Example

1  PackageCom.mooger.message;2 3 ImportAndroid.os.Parcel;4 Importandroid.os.Parcelable;5 6  Public classSkymessageImplementsparcelable {7      PublicString Floder;8      Public intaid;9      Public Longdelay;Ten      PublicString head; One      PublicString body; A      PublicString Foot; -      -      PublicSkymessage (String Floder,intAid,Longdelay, string head, string body, string foot) the     { -          This. Floder =Floder; -          This. AID =aid; -          This. Delay =delay; +          This. Head =head; -          This. BODY =body; +          This. Foot =foot; A     } at      - @Override -      Public intdescribecontents () { -         return0;  -     }   -      in @Override -      Public voidWritetoparcel (Parcel dest,intflags) {   to dest.writestring (floder); + Dest.writeint (aid); - Dest.writelong (delay); the dest.writestring (head); * dest.writestring (body); $ dest.writestring (foot);Panax Notoginseng     }   -  the      Public Static FinalParcelable.creator<skymessage> Creator =NewParcelable.creator<skymessage>(){   + @Override A          Publicskymessage Createfromparcel (Parcel source) { the             return NewSkymessage (source.readstring (), Source.readint (), Source.readint (), source.readstring (), source.readstring (),  Source.readstring ());  +         }   -          $ @Override $          PublicSkymessage[] NewArray (intsize) {   -             return NewSkymessage[size];  -         }   the     }; - Wuyi}

Then, you need to build a aidl file with the same name under the same package for Android to generate the appropriate auxiliary files:

1  Package Com.mooger.message; 2 3 parcelable skymessage;

Here are two aidl files

Skyactivity.aidl

1  Package Com.mooger.aidl; 2 3 Interface skyactivity{4     void downloadstate (String TAG,int  AID); 5 }

Skyservice.aidl

1  PackageCom.mooger.aidl;2 3 Importcom.mooger.aidl.SkyActivity;4 ImportCom.mooger.message.SkyMessage;5 6 Interfaceskyservice{7     voidDownload (string download_url, String Appname,intDownload_size,intAID, String floder);8     voidRequestintTime );9     voidRegisterskycall (skyactivity at);Ten     voidCreatad (in Skymessage message); One}

Implement an inner class inheritance Skyservice in your service class and implement the corresponding method

1      Public classServicebinderextendsskyservice.stub{2 3 @Override4          Public voidDownload (string download_url, String Appname,5                 intDownload_size,intAID, String Floder)throwsRemoteException {6             //TODO auto-generated Method Stub7             .....8         }9 Ten @Override One          Public voidRegisterskycall (skyactivity at)throwsRemoteException { A             //TODO auto-generated Method Stub -             ...... -         } the  - @Override -          Public voidRequestintTimethrowsRemoteException { -             //TODO auto-generated Method Stub +             ....... -         } +  A @Override at          Public voidCreatad (Skymessage message)throwsRemoteException { -             //TODO auto-generated Method Stub -             ......... -         }     -}

Creating an Servicebinder instance

1 Private New Servicebinder ();

and returns the instance in the Onbind method

1 @Override 2  Public ibinder onbind (Intent Intent) {3       LOG.D (TAG, "Service onbind"); 4         return Servicebinder; 5     }

Instantiate a Serviceconnection object in activity and implement the appropriate method

1     PrivateSkyservice Downloadservice;2     3     PrivateServiceconnection serviceconnection =Newserviceconnection () {4 5 @Override6          Public voidonserviceconnected (componentname name, IBinder service) {7             //TODO auto-generated Method Stub8LOG.D ("Bootstart", "Activity onserviceconnected");9Downloadservice =SkyService.Stub.asInterface (service);Ten             Try { One Downloadservice.registerskycall (atcallback); A}Catch(RemoteException e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); the             } -         } -  - @Override +          Public voidonservicedisconnected (componentname name) { -             //TODO auto-generated Method Stub +Downloadservice =NULL; A         } at          -};

Finally, the binding is implemented by the Bindservice method

1 this. Bindservice (newserviceconnection, bind_auto_create); // binding to a service

Service settings in the androidmanifest.xml

1 <ServiceAndroid:name= "Com.mooger.service_133.MooService"Android:label= "@string/service_name"2 Android:icon= "@drawable/ic_launcher">3        <Intent-filter>4         <ActionAndroid:name= "Com.mooger.aidl.Service" /> 5     </Intent-filter>            6 </Service>

This completes the basic setup and deployment, as long as the communication pass data to the following example code can be

1Skymessage message =NewSkymessage (folder, Jsonobj.optint ("aid"), Jsonobj.optlong ("delay"), jsonobj.optstring ("Head"), Jsonobj.optstring (" Body "), jsonobj.optstring (" foot "));2 Try {    3 Downloadservice.creatad (message); 4}Catch(RemoteException e) {5     //TODO auto-generated Catch block6 e.printstacktrace ();7}

Android service activity interacts with remote services that pass complex data types

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.