Miguel Ortega
Miguel Ortega

Reputation: 109

Passing extras through PendingIntent is not working

I'm building a task based application for Android.

I need to pass a task object from setAlarm method to onReceive method to configure custom alarms, but onReceive is getting null from the bundle:

Bundle bundle = intent.getExtras();
Task task = (Task) bundle.getSerializable("task");

Here is my code:

private AlarmManager alarmMgr;

private PendingIntent alarmIntent;


@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    Task task = (Task) bundle.getSerializable("task");

    Intent service = new Intent(context, SchedulingService.class);
    service.putExtra("task", task);

    startWakefulService(context, service);

}

public void setAlarm(Context context, Task task) {
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("task", task);
    intent.setAction("com.blah.Action");
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Calendar calendar = Calendar.getInstance();
    try {
        Date date = format.parse(task.getDate());

        calendar.setTime(date);
        StringTokenizer stringTokenizer = new StringTokenizer(task.getTime(), ":");
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(stringTokenizer.nextToken()));
        calendar.set(Calendar.MINUTE, Integer.parseInt(stringTokenizer.nextToken()));

    } catch (ParseException e) {
        e.printStackTrace();
    }


    alarmMgr.set(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), alarmIntent);

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);

}

What's wrong with it?

EDIT

I have tried to pass a string and an int, and works in both cases. Is there any problem with my task class?

public class Task implements Serializable {

private int id;


private String name;
private String date;
private String time;
private String date_creation;
private String description;
private String location;
private String image;
private String state;
private String state2;
private String profile;

public Task() {

}

public Task(int id, String name, String date, String time, String date_creation, String description, String location, String image, String state, String state2, String profile) {
    this.setId(id);
    this.setName(name);
    this.setDate(date);
    this.setTime(time);
    this.setDate_creation(date_creation);
    this.setDescription(description);
    this.setLocation(location);
    this.setImage(image);
    this.setState(state);
    this.setState2(state2);
    this.setProfile(profile);
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getState2() {
    return state2;
}

public void setState2(String state2) {
    this.state2 = state2;
}

public String getProfile() {
    return profile;
}

public void setProfile(String profile) {
    this.profile = profile;
}

public String getDate_creation() {
    return date_creation;
}

public void setDate_creation(String date_creation) {
    this.date_creation = date_creation;
}

}

The intent in setAlarm have the extras, but in onReceive is empty.

I have tried passing task id as unique requestCode, with no success.

I'm launching the app in API 22

Upvotes: 1

Views: 556

Answers (2)

Gentle
Gentle

Reputation: 559

It may not be the problem with your device. I think that the ParcelableExtra is getting dropped in case of broadcast receiver! I too was passing it using PendingIntent, BUT, when the broadcast receiver fired off the activity, it did not re-attach the extras, resulting in a null in the activity when it tried to getParcelableExtras.

Upvotes: 1

Mark
Mark

Reputation: 9929

you could try using the Parcelable interface :

public class Task implements Parcelable {

    private int id;


    private String name;
    private String date;
    private String time;
    private String date_creation;
    private String description;
    private String location;
    private String image;
    private String state;
    private String state2;
    private String profile;

    public Task() {

    }

    public Task(int id, String name, String date, String time, String date_creation, String description, String location, String image, String state, String state2, String profile) {

        this.id = id;
        this.name = name;
        this.date = date;
        this.time = time;
        this.date_creation = date_creation;
        this.description = description;
        this.location = location;
        this.image = image;
        this.state = state;
        this.state2 = state2;
        this.profile = profile;
    }

    protected Task(Parcel in) {

        id = in.readInt();
        name = in.readString();
        date = in.readString();
        time = in.readString();
        date_creation = in.readString();
        description = in.readString();
        location = in.readString();
        image = in.readString();
        state = in.readString();
        state2 = in.readString();
        profile = in.readString();
    }

    public static final Creator<Task> CREATOR = new Creator<Task>() {
        @Override
        public Task createFromParcel(Parcel in) {
            return new Task(in);
        }

        @Override
        public Task[] newArray(int size) {
            return new Task[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(id);
        dest.writeString(name);
        dest.writeString(date);
        dest.writeString(time);
        dest.writeString(date_creation);
        dest.writeString(description);
        dest.writeString(location);
        dest.writeString(image);
        dest.writeString(state);
        dest.writeString(state2);
        dest.writeString(profile);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState2() {
        return state2;
    }

    public void setState2(String state2) {
        this.state2 = state2;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getDate_creation() {
        return date_creation;
    }

    public void setDate_creation(String date_creation) {
        this.date_creation = date_creation;
    }
}

Then use to put in an bundle : putParcelable("task", myTask);

and to retrieve:

Task myTask = (Task) bundle.getParcelable("task");

Upvotes: 1

Related Questions