Aidl 2: aidl service for transmitting complex data

Source: Internet
Author: User

The aidl service only supports limited data types. If you use the aidl service to transmit complex data, you need to perform further processing. The aidl Service supports the following data types:
1. Java Native type
2. String and charsequence
3. The elements of list, MAP, list, and map objects must be data types supported by aidl. The preceding three types do not need to be imported)
4. the API automatically generated by aidl needs to be imported)
5. classes that implement the Android. OS. parcelable interface. Import is required ).

To pass a value of the data type to be imported (for example, to implement the parcelable Interface Class), apart from creating a class to implement the parcelable interface, you also need to create an aidl file for this class and define it using the parcelable keyword.

I. Server Side

The procedure is as follows:

1. Create an imyservice. aidl file. The Code is as follows:

Package net. blogjava. Mobile. Complex. type. aidl;

Import net. blogjava. Mobile. Complex. type. aidl. produce;

Interface imyservice {

Map getmap (in string country, in product );

Product getproduct ();

}

Note: If the type of a method parameter is not a native type, such as string, list, or custom object class, you must use in, out, or inout to modify it. In indicates that this value is set by the client; "Out" indicates that the value is set by the server, and "inout" indicates that the value is set by both the client and the server.

2. compile the product class, which is the data type used for transmission. It implements the parcelable interface and is used to serialize objects. io. serializable interface, because the serialization efficiency of the serializable interface is not as high as that of the parcelable Interface

(1) There must be a static constant in the product class, the constant name must be creator, and the Data Type of the Creator constant must be parcelable. Creator

(2) write the value to be serialized to the parcel object in the writetoparcel method.

3. Create a product. aidl file with the code: parcelable product;

4. Compile a myservice class

5. Configure the myservice class in the androidmanifest. xml file. The Code is as follows:

<Service android: Name = ". myservice">

<Intent-filter> <action Android: Name = "net. blogjava. Mobile. Complex. type. aidl. imyservice"/> </intent-filter>

</Service>

Ii. Client

Copy the imyservice. Java and product. java files to the client project (ch08_complextypeaidlclient), bind the aidl service, obtain the aidl service object, and call the aidl service method.

When running, first run the server, then run the client program, click the BIND aidl Service button, and then click the call aidl Service button.

For specific code, seeCh08_complextypeaidl, ch08_complextypeaidlclient
Engineering

Share:

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.