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