android - 广播 PendingIntent 和 AlarmManager.setAlarmClock() 会丢失额外的 Intent

标签 android android-intent android-pendingintent android-broadcast android-alarms

我像这样创建一个 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_CURRENTFLAG_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/

相关文章:

android - 有没有办法将签名的 apk 上传到 crashlytics?

android - 刷新 PopupWindow.dismiss 上的父 fragment

android解锁屏幕 Intent ?

android - 将 Activity 结果传回祖 parent Activity (不是父 Activity )

Android - 使用带有通知的未决 Intent

javascript - Jquery 在 Android 上运行不佳

android - 如何在 Retrofit 2 (Android) 中动态设置标题列表

android - 将整数传递给 BaseAdapter

android - future 通知的时间戳不正确

android - 重新启动应用程序失败 android