Alarm PendingIntent 发送的 Intent 在 Bundle 中有一个空的 Parcelable

Intent sent by Alarm PendingIntent has a null Parcelable in the Bundle

我正在尝试通过警报未决意图发送捆绑包,但可打包数据似乎始终为空。变量 hsr 是一个 Parcelable 对象。这是我的代码:

for (final HolderSubjectReminder hsr : alHolderSubjectReminders) {


  int hours = 23;
  int minutes = 0;
  DateTime alarmDate = new DateTime().dayOfMonth().roundFloorCopy().plusHours(hours).plusMinutes(minutes);
  Intent intent = new Intent("com.mycompany.ediary");
  Bundle bundle = new Bundle();
  bundle.putParcelable("Reminder", hsr);
  intent.putExtras(bundle);

  pendingIntent = PendingIntent.getBroadcast(con, count, intent, 0);
  alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getMillis(), pendingIntent);
}

在我的 BroadcastReceiver 中(在正确的时间接收广播):

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

        Bundle bundle = intent.getExtras(); //not null
        if (bundle != null) {

            HolderSubjectReminder hsr = bundle.getParcelable("Reminder"); //null
            if (hsr != null) {
                subjectReminders.shouldWeShowAlert(hsr);
            }
        }
     }

在广播接收器中检索变量 hsr 时始终为 null。我究竟做错了什么?顺便说一下,bundle 也不为空。

也许您创建的待处理 intent 多于应用中的 intent。在这种情况下,您应该使用 , PendingIntent.FLAG_CANCEL_CURRENT 从头开始​​创建 Intent 并扩充您的包:

示例:

pendingIntent = PendingIntent.getBroadcast(con, count, intent, PendingIntent.FLAG_CANCEL_CURRENT);

旗帜信息:

http://developer.android.com/intl/es/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT