1

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

Miguel Ortega
  • 109
  • 1
  • 9
  • whats the android version for the emulator ? – Mohammad Salem Dec 31 '15 at 17:01
  • what `Task` class do you use? – Floern Dec 31 '15 at 17:45
  • I couldn't figure this out by just reading. A couple of suggestions - (1) inspect the extras of the intent right after putting the task extra, and then inspect the extras of the intent at the very beginning of onRecieve, (2) try passing a simpler extra, like an int or string (I assume Task is indeed Serializable) and see what happens. I would be interested at the result of these. – Ginandi Dec 31 '15 at 17:46
  • when calling `getBroadcast` pass a unique `requestCode` – pskink Dec 31 '15 at 18:10
  • just for fun, assign the variables directly in the Task constructor i.e. this.id = id; rather than through the setter methods, in fact if you don't need to change the variables once assigned in the constructor don't bother with the setter methods .. worth a try. – Mark Jan 01 '16 at 20:39
  • I need the setter methods for other classes, but I appreciate your suggestion Mark :) – Miguel Ortega Jan 01 '16 at 20:47

2 Answers2

1

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");
Mark
  • 9,604
  • 5
  • 36
  • 64
  • I have still the same problem :( – Miguel Ortega Jan 02 '16 at 00:08
  • Well obvious test to rule out the Task object would be to pass it between 2 activities and see if that works - I'm sure it will. Something else is going on elsewhere in the code. this link night have some insights : http://stackoverflow.com/a/10685132/4252352 – Mark Jan 02 '16 at 00:47
  • With normal intents it works. I have tried in other 3 devices (two API's 22 and one 19) , and works perfectly, so the problem is my device (API 22). So weird. – Miguel Ortega Jan 03 '16 at 11:58
1

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.

Gentle
  • 519
  • 6
  • 12