Implementing a Parcelable Class Android

In the example so far, we have been lucky in that the Location class implements the Parcelable interface. What if a new object needs to be passed through a remote interface?

Let’s take the following class, GPXPoint, as an example:

public final class GPXPoint {
public int latitude;
public int longitude;
public Date timestamp;
public double elevation;
public GPXPoint() {
}
}

The GPXPoint class defines a location point that is similar to a GeoPoint but also includes the time the location was recorded and the elevation. This data is commonly found in the popular GPX file format. On its own, this is not a basic format that the system recognizes to pass through a remote interface. However, if the class implements the Parcelable interface and we then create an AIDL file from it, the object can be used in a remote interface.

To fully support the Parcelable type, we need to implement a few methods and a Parcelable.Creator<GPXPoint>.The following is the same class now modified to be a Parcelable class:

public final class GPXPoint implements Parcelable {public int latitude; public int longitude; public Date timestamp; public double elevation; public static final Parcelable.Creator<GPXPoint> CREATOR = new Parcelable.Creator<GPXPoint>() { public GPXPoint createFromParcel(Parcel src) { return new GPXPoint(src); } public GPXPoint[] newArray(int size) { return new GPXPoint[size]; }

};
public GPXPoint() {
}
private GPXPoint(Parcel src) {
readFromParcel(src);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(latitude);
dest.writeInt(longitude);
dest.writeDouble(elevation);
dest.writeLong(timestamp.getTime());
}
public void readFromParcel(Parcel src) {
latitude = src.readInt();
longitude = src.readInt();
elevation = src.readDouble();
timestamp = new Date(src.readLong());
}
public int describeContents() {
return 0;
}
}

The writeToParcel() method is required and flattens the object in a particular order using supported primitive types within a Parcel. When the class is created from a Parcel, the Creator is called, which, in turn, calls the private constructor. For readability, we also created a readFromParcel() method that reverses the flattening, reading the primitives in the same order that they were written and creating a new Date object.

Now you must create the AIDL file for this class. You should place it in the same directory as the Java file and name it GPXPoint.aidl to match. You should make the contents look like the following:

package com.androidbook.services;
parcelable GPXPoint;

Now the GPXPoint class can be used in remote interfaces. This is done in the same way as any other native type or Parcelable object. You can modify the IRemote Interface .aidl file to look like the following:

package com.androidbook.services;
import com.androidbook.services.GPXPoint;
interface IRemoteInterface {
Location getLastLocation();
GPXPoint getGPXPoint();
}

Additionally,we can provide an implementation for this method within the interface, as follows:

public GPXPoint getGPXPoint() {
if (lastLocation == null) {
return null;
} else {
Log.v(“interface”, “getGPXPoint() called”);
GPXPoint point = new GPXPoint();
point.elevation = lastLocation.getAltitude();
point.latitude =
(int) (lastLocation.getLatitude() * 1E6);
point.longitude =
(int) (lastLocation.getLongitude() * 1E6);
point.timestamp =
new Date(lastLocation.getTime());
return point;
}
}

As can be seen, nothing particularly special needs to happen. Just by making the object Parcelable, it can now be used for this purpose.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics