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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…