Passing custom parameters between processes in Android

Source: Internet
Author: User

[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:

 

 

  1. Public class person implements parcelable
  2. Private integer ID;
  3. Private string name;
  4. Public Person (){}
  5. Public Person (integer ID, string name ){
  6. This. ID = ID;
  7. This. Name = Name;
  8. }
  9. Public integer GETID (){
  10. Return ID;
  11. }
  12. Public void setid (integer ID ){
  13. This. ID = ID;
  14. }
  15. Public String getname (){
  16. Return name;
  17. }
  18. Public void setname (string name ){
  19. This. Name = Name;
  20. }
  21. @ Override
  22. Public int describecontents (){
  23. Return 0;
  24. }
  25. @ Override
  26. Public void writetoparcel (parcel DEST, int flags) {// write data in javanbean to parcel
  27. DeST. writeint (this. ID );
  28. DeST. writestring (this. Name );
  29. }
  30. // Add a static member named Creator, which implements the parcelable. Creator interface.
  31. Public static final parcelable. creator <person> creator = new parcelable. creator <person> (){
  32. @ Override
  33. Public Person createfromparcel (parcel source) {// reads data from parcel and returns the person object
  34. Return new person (source. readint (), source. readstring ());
  35. }
  36. @ Override
  37. Public person [] newarray (INT size ){
  38. Return new person [size];
  39. }
  40. };
  41. }

 

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:

  1. Package com. Hoo. aidl;
  2. Import CN. itcast. domain. person;
  3. Interface ipersonservice {
  4. Void save (in person );
  5. }

 

 

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:

  1. Public class servicebinder extends ipersonservice. Stub {
  2. @ Override
  3. Public void save (person) throws RemoteException {
  4. Log. I ("personservice", person. GETID () + "=" + person. getname ());
  5. }
  6. }

 

 

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:

  1. Public class personservice extends Service {
  2. Private servicebinder = new servicebinder ();
  3. @ Override
  4. Public ibinder onbind (intent ){
  5. Return servicebinder;
  6. }
  7. Public class servicebinder extends ipersonservice. Stub {
  8. @ Override
  9. Public void save (person) throws RemoteException {
  10. Log. I ("personservice", person. GETID () + "=" + person. getname ());
  11. }
  12. }
  13. }

 

Other applications can access the service by implicit intent, and the intent actions can be customized. The androidmanifest. xml configuration code is as follows:

  1. <Service android: Name = ". personservice">
  2. <Intent-filter>
  3. <Action Android: Name = "com. Hoo. process. aidl. personservice"/>
  4. </Intent-filter>
  5. </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:

  1. Public class clientactivity extends activity {
  2. Private ipersonservice personservice;
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. Main );
  7. This. bindservice (new intent ("cn. itcast. process. aidl. personservice"), this. serviceconnection, bind_auto_create); // bind to the service
  8. }
  9. @ Override
  10. Protected void ondestroy (){
  11. Super. ondestroy ();
  12. This. unbindservice (serviceconnection); // remove the service
  13. }
  14. Private serviceconnection = new serviceconnection (){
  15. @ Override
  16. Public void onserviceconnected (componentname name, ibinder Service ){
  17. Personservice = ipersonservice. stub. asinterface (service );
  18. Try {
  19. Personservice. Save (new person (56, "liming "));
  20. } Catch (RemoteException e ){
  21. Log. E ("clientactivity", E. tostring ());
  22. }
  23. }
  24. @ Override
  25. Public void onservicedisconnected (componentname name ){
  26. Personservice = NULL;
  27. }
  28. };
  29. }

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.