Digital Da
Digital Da

Reputation: 911

Passing variables in and out of GCM task service

I am using the GCM network manager and I want to pass the service (specifically to the onRunTask(TaskParams taskParams) some objects. From the documentation taskParams are simply a string and a bundle but I want to pass more complex objects.

How can this be done?

Thank you!

Upvotes: 1

Views: 260

Answers (1)

Silvestr
Silvestr

Reputation: 809

One way is to have your custom object implement the Parcelable interface and use Bundle.putParcelable/Bundle.getParcelable.

It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).

For example:

public class MyParcelable implements Parcelable {
 private int mData;

 public int describeContents() {
     return 0;
 }

 public void writeToParcel(Parcel out, int flags) {
     out.writeInt(mData);
 }

 public static final Parcelable.Creator<MyParcelable> CREATOR
         = new Parcelable.Creator<MyParcelable>() {
     public MyParcelable createFromParcel(Parcel in) {
         return new MyParcelable(in);
     }

     public MyParcelable[] newArray(int size) {
         return new MyParcelable[size];
     }
 };

 private MyParcelable(Parcel in) {
     mData = in.readInt();
 }

}

Also you can read Parcelable vs Serializable

Upvotes: 1

Related Questions