Search code examples
androidandroid-intentparcelable

Read parcelable not working while writing does


I am send data from one service to other service using parecable, when I write data contains proper value but when read data from parcelable it return null values.

can any one tell me what is the problem with this code?

Parcelable class

package com.airwatch.agent.finddevice;

import android.os.Parcel;
import android.os.Parcelable;


/**
 * Represents the configuration supplied from device services 
 * regarding the  find device command.
 */
public class FindDeviceConfig implements Parcelable{
    public FindDeviceConfig() {
    }

    /**
     * number of loops to play ring-tone.
     */
    private String numberoftime;

    /**
     * Delay between loops - in seconds.
     */
    private String interval;

    public FindDeviceConfig(String numberoftime, String interval) {
        this.numberoftime = numberoftime;
        this.interval = interval;
    }

    public String getNumberoftime() {
        return numberoftime;
    }

    public void setNumberoftime(String numberoftime) {
        this.numberoftime = numberoftime;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        //dest.writeInt(Integer.parseInt(numberoftime));

        //dest.writeInt(Integer.parseInt(interval));
        dest.writeString(this.interval);
        dest.writeString(this.numberoftime);
    }

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

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

    public FindDeviceConfig(Parcel parcel)
    {
        parcel.setDataPosition(0);

        interval=parcel.readString();
        numberoftime=parcel.readString();

    }
}

Putting the parcelable object.

FindDeviceConfig config=new FindDeviceConfig("3","2");
intent.putExtra("finddevice_config", config);

Reading the parcelable object.

FindDeviceConfig config = intent.getParcelableExtra("finddevice_config");

Anyone faced this problem?


Solution

  • add the following constructor:

    public FindDeviceConfig(Parcel data) {
        this.numberoftime  = data.readString();
        this.interval = data.readString();
     }