我像这样创建一个 PendingIntent
:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("Foobar");
intent.putExtra(EXTRA_ALARM, alarm);
intent.putExtra(EXTRA_TRYTWO, tryTwo);
intent.putExtra(EXTRA_BEGAN_TIME, beganTime);
return PendingIntent.getBroadcast(
context, (int) alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT
);
alarm
变量是一个Parcelable
。我这样安排 PendingIntent
:
PendingIntent alarmModifyPendingIntent = PendingIntent.getActivity(
context, 0, editIntent, PendingIntent.FLAG_CANCEL_CURRENT
);
am.setAlarmClock(
new AlarmManager.AlarmClockInfo(time, alarmModifyPendingIntent), pendingIntent
);
如上所示创建变量 pendingIntent
的位置。
AlarmReceiver
对象在正确的时间在 onReceive
中接收到一个 Intent
。但是,此 Intent
不包含我设置的额外内容。例如 intent.getParcelableExtra(EXTRA_ALARM)
返回 null
。
至少在使用 LG G5 的 Android 7.0(API 级别 24)上会出现此问题。
使用 FLAG_CANCEL_CURRENT
或 FLAG_ONE_SHOT
也不起作用。
最佳答案
The alarm variable is a Parcelable.
It is not safe to put a custom Parcelable
in an Intent
that is delivered to another process .对于 Android 7.0 上的 AlarmManager
尤其如此。
您需要将 Parcelable
替换为其他内容,例如 byte[]
,其中 you manually convert your Parcelable
to/from that byte[]
.
关于android - 广播 PendingIntent 和 AlarmManager.setAlarmClock() 会丢失额外的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131710/