Pass by value Parameters using Parcelables

If you have a class that you would like to send from one process to another through an AIDL interface, you can do that. You must ensure that the code for your class is available to the other side of the IPC. Generally, that means that you're talking to a service that you started.

There are five parts to making a class support the Parcelable protocol:

1. Make your class implement the Parcelable interface.

2. Implement the method public void writeToParcel(Parcel out) that takes the current state of the object and writes it to a parcel.

3. Implement the method public void readFromParcel(Parcel in) that reads the value in a parcel into your object.

4. Add a static field called creator to your class which is an object implementing the Parcelable.Creator interface.

5. Last but not least:

o If you are developing with Eclipse/ADT, follow these steps:

a. In the Package Explorer view, right-click on the project.

b. Choose Android Tools > Create Aidl preprocess file for Parcelable classes.

c. This will create a file called "project.aidl" in the root of the project. The file will be automatically used when compiling an aidl file that uses the parcelable classes.

o If you are developing with Ant or are using a custom build process, create an aidl file that declares your parcelable class (as shown below). If you are using a custom build process, do not add the aidl file to your build. Similar to a header file in C, the aidl file isn't compiled.

AIDL will use these methods and fields in the code it generates to marshall and unmarshall your objects.

Here 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];

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();

Here is Rect.aidl for this example package android.graphics;

// Declare Rect so AIDL can find it and knows that it implements // the parcelable protocol, parcelable Rect;

The marshalling in the Rect class is pretty simple. Take a look at the other methods on Parcel to see the other kinds of values you can write to a Parcel.

Warning: Don't forget the security implications of receiving data from other processes. In this case, the rect will read four numbers from the parcel, but it is up to you to ensure that these are within the acceptable range of values for whatever the caller is trying to do. See Security and Permissions for more on how to keep your application secure from malware.

0 0

Post a comment