android– 使用广播PendingIntent和AlarmManager.setAlarmClock()丢失Intent额外内容

android– 使用广播PendingIntent和AlarmManager.setAlarmClock()丢失Intent额外内容,第1张

概述我像这样创建一个PendingIntent:Intentintent=newIntent(context,AlarmReceiver.class);intent.setAction("Foobar");intent.putExtra(EXTRA_ALARM,alarm);intent.putExtra(EXTRA_TRYTWO,tryTwo);intent.putExtra(EXTRA_BEGAN_TIME,beganTime);returnPendi

我像这样创建一个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 – 使用广播PendingIntent和AlarmManager.setAlarmClock()丢失Intent额外内容全部内容,希望文章能够帮你解决android – 使用广播PendingIntent和AlarmManager.setAlarmClock()丢失Intent额外内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存