Implement Parcelable interface by passing custom parameters between Android 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 (Parceldest, intflags) method of the Parcelable interface. 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. For the implementation process of passing custom types between processes, see the following: 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: [java] package cn. itcast. domain; import android. OS. parcel; import android. OS. parcelable; 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 g EtName () {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 Parcelable. creator interface public static final Parcelable. creator <Person> CREATOR = new Parcelable. crea Tor <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 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 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 code that implements the interface method. The content is as follows: public class ServiceBinder extendsIPersonService. stub {@ Override public void save (Person person) throwsRemoteException {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: [java] public class PersonService extends Service {private ServiceBinder serviceBinder = new ServiceBinder (); @ Override public IBinder onBind (Intent intent) {return serviceBinder ;} public class ServiceBinder extends IPersonService. stub {@ Override public void save (Person person) throws RemoteException {Log. I ("PersonService", person. getId () + "=" + person. getName () ;}} other applications can access the service by implicit intent. The action can be customized, AndroidManifest. the xml configuration code is as follows: <serviceandroid: name = ". personService "> <intent-filter> <actionandroid: 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: [java] 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 service} @ Override protected void onDestroy () {super. onDestroy (); this. unbindService (serviceConnection); // remove service} private ServiceConnection serviceConnection = new ServiceConnection () {@ Override public 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 () ;}@ Override public void onServiceDisconnected (ComponentName name) {personService = null ;}};}

Related Article

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.