android – 使用广播PendingIntent和AlarmManager.setAlarmClock()丢失Intent额外内容
作者:互联网
我像这样创建一个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
);
警报变量是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.
Android 7.0(API级别24)至少使用LG G5会出现此问题.
使用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-broadcast,android,android-intent,android-alarms,android-pendingintent 来源: https://codeday.me/bug/20190724/1523922.html