Passing custom parameters between processes

Source: Internet
Author: User
By default, aidl supports 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 implements the public void writetoparcel (parcel DEST, int flags) method of the parcelable interface. 2> the custom type must contain a static member named creator. The member object must be 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. example: 1> create a custom type and implement the parcelable interface so that it supports the parcelable protocol. for example, in CN. itcast. create person under the domain package. java: Package CN. itcast. domain; import android. OS. parcel; import android. OS. parcelable; public class person implements parcelableprivate 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 ;}@ overridepublic int describecontents () {return 0 ;}@ overridepublic void writetoparcel (parcel DEST, int flags) {// write data in javanbean to parceldest. writeint (this. ID); DeST. writestring (this. name);} // Add a static member named Creator, which implements parcelable. creator interface public static final parcelable. creator <person> creator = new parcelable. creator <person> () {@ overridepublic person createfromparcel (parcel source) {// reads data from parcel and returns the person object return new person (source. readint (), source. readstring () ;}@ overridepublic 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 CN. itcast. domain; parcelable person; 3> to use a custom type in the aidl file of the interface, you must use import to explicitly import the data. In this example. itcast. create ipersonservice under the aidl package. the aidl file contains the following content: Package CN. itcast. 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, instead, it is implemented through the stub of the inherited interface (the aidl interface is implemented inside the stub abstract class) and the interface method is implemented. Code . 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 (intent) method of the service, return the object that implements the aidl interface (servicebinder in this example ). the content is as follows: public class personservice extends Service {private servicebinder = new servicebinder (); @ overridepublic 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. The intent actions can be customized, androidmanifest. the xml configuration code is as follows: <service android: Name = ". personservice "> <intent-filter> <action Android: Name =" CN. itcast. 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 implement communication with remote services in the client application. The Code is as follows: public class clientactivity extends activity {private ipersonservice personservice; @ overridepublic 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 service} @ overrideprotected void ondestroy () {super. ondestroy (); this. unbindservice (serviceconnection); // remove service} private serviceconnection = new serviceconnection () {@ overridepublic void onserviceconnected (componentname, ibinder Service) {personservice = ipersonservice. stub. asinterface (service); try {personservice. save (new person (56, "liming");} catch (RemoteException e) {log. E ("clientactivity", E. tostring () ;}@ overridepublic void onservicedisconnected (componentname name) {personservice = NULL ;}};}

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.