Android Interface Definition Language-aidl (III)

Source: Internet
Author: User

Passing objects on the IPC Channel

This can be done if you want to send a class from one process to another through the IPC interface. However, make sure that the code of this class corresponds to the process on the other side of the IPC channel is available, and this class must support the parcelable interface. It is important to support the parcelable interface because it allows the Android system to break down objects into primitive types that can be grouped across processes.

Follow these steps to create a class that supports the parcelable protocol:

1. Implement the parcelable interface for this class;

2. Implement the writetoparcel interface, which carries the current state of the object and writes it to parcel;

3. Add a field named creator to this class, which is an object that implements the parcelable. Creator interface;

4. Finally, create a. aidl file (the rect. aidl file shown below) that declares the package class );

If you want to use a customized compilation process without adding the. aidl file, this. aidl file will not be compiled, similar to the header file in C language.

Aidl uses the methods and fields in the code it generates to group and group the object.

For example, the rect. aidl file creates a rect class that can be packaged:

Package Android. graphics;

// Declare rect so aidl can find it and knows that it implements

// The parcelable protocol.

Parcelable rect;

The following is an example of how the rect class implements the parcelable protocol:

Import Android. OS. parcel;

Import Android. OS. parcelable;

Public final class rect implements parcelable {

Public int left;

Public int top;

Public int right;

Public int bottom;

 

Public static final parcelable. creator <rect> creator = new

Parcelable. creator <rect> (){

Public rect createfromparcel (parcel in ){

Return new rect (in );

}

 

Public rect [] newarray (INT size ){

Return new rect [size];

}

};

 

Public rect (){

}

 

Private rect (parcel in ){

Readfromparcel (in );

}

 

Public void writetoparcel (parcel out ){

Out. writeint (left );

Out. writeint (top );

Out. writeint (right );

Out. writeint (bottom );

}

 

Public void readfromparcel (parcel in ){

Left = in. readint ();

Top = in. readint ();

Right = in. readint ();

Bottom = in. readint ();

}

}

Grouping in the rect class is quite simple. You can compile a parcel class by looking at some other methods on parcel and their value types.

Warning do not forget the security impact of receiving data from another process. In this example, the rect class reads four numbers from parcel, but no matter what the caller attempts to do, you must ensure the range of values that can be received. For more information about how to protect software against malicious attacks, see "security and permissions"

Http://developer.android.com/guide/topics/security/permissions.html

 

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.