Here is the problem. My program is running perfect in Android 6.0. After update the device to android 7.0. Pendingintent can not pass the parcelable data to boradcast reveiver. Here is the code.
Fire the alarm
public static void setAlarm(@NonNull Context context, @NonNull Todo todo) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("KEY_TODO", todo);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, todo.remindDate.getTime(), alarmIntent);
}
Todo is a Parcelable class while todo is the instance I need in notification.
In Broadcastreceiver, I cannot getParcelable data.
public void onReceive(Context context, Intent intent) {
Todo todo = intent.getParcelableExtra("KEY_TODO");
}
Here is the result of intent when I debug
I dont know why the intent only contains a Integer that I never put it in. Where is the Parcelable todo. This code has no problem in android 6.0, but can not run in 7.0
Todo
object in aBundle
before adding it to the "extras"? This usually works when passing customParcelable
objects to theAlarmManager
(but may now be broken in Android 7). I would be interested in your findings. – LedouxBundle bundle = new Bundle; bundle.putParcelable("todo", todo); intent.putExtra("KEY_TODO", bundle);
. To extract extra:Bundle bundle = intent.getBundleExtra("KEY_TODO"); if (bundle != null) { Todo todo = bundle.getParcelableExtra("todo"); }
– Ledoux