如何正确地将独特的额外内容传递给待处理的意图?
我遇到了 alarmManager
以及与之相关的附加意图的问题。
如果我设置多个闹钟,它们就会响起,但附加功能保持不变。
我已经读过这些问题:
我已经尝试过:
- 为每个待处理意图分配一个唯一的 ID 并
- 使用所有待处理意图标志,
所有这些都用于没有用。我不知道为什么它不起作用。
这是一个代码片段:
Intent intent = new Intent(con,
AppointmentNotificationReciever.class);
intent.putExtra("foo", bar.toString());
int id = randomNum;
PendingIntent sender = PendingIntent.getBroadcast(con, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, scheduleExecution, sender);
I'm having a problem with alarmManager
and the pending intent with extras that will go along with it.
If I set multiple alarms, they will go off, however the extras stay the same.
I have already read into these questions:
- android pending intent notification problem
- Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?
and I have tried:
- assigning a unique ID to each pending intent and
- using all the pending intent flags,
all to no avail. I have no clue why it will not work.
Here is a code snippet:
Intent intent = new Intent(con,
AppointmentNotificationReciever.class);
intent.putExtra("foo", bar.toString());
int id = randomNum;
PendingIntent sender = PendingIntent.getBroadcast(con, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, scheduleExecution, sender);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里可能有两个不同的问题:
1)如果您之前已经创建了 PendingIntent 并且它“匹配”现有的 PendingIntent,那么您必须指定 PendingIntent.FLAG_UPDATE_CURRENT 标志,否则它将不会传递额外内容。 “匹配”基于 Intent.filterEquals() 使用的标准,因此一定要阅读那里的文档并确保您了解数据、操作、类型等。
2) 我已经读到,如果您不设置对你的意图采取行动,那么它就不会传播额外的内容,所以也许可以尝试intent.setAction(“com.blah.Action”)。
Possibly two different issues here:
1) If you've already created your PendingIntent before and it "matches" an existing PendingIntent, then you must specify the PendingIntent.FLAG_UPDATE_CURRENT flag or it will not pass the extras. A "match" is based on the criteria that Intent.filterEquals() uses, so definitely read the docs there and make sure you understand the data, action, type, etc.
2) I've read that if you do NOT set an action on your intent, then it will not propagate the extras, so perhaps try intent.setAction("com.blah.Action").
我遇到了类似的问题。使用 PendingIntent.FLAG_ONE_SHOT 可能会解决问题,因为这意味着 PendingActivity 不会被重用。
I've run into a similar problem. Using PendingIntent.FLAG_ONE_SHOT may solve the problem, because it means the PendingActivity won't be reused.
这可能是由于 Activity::getIntent 在给定某些意图标志/过滤器的情况下返回了 Activity 的原始意图。
如果您是这种情况,则需要查看 Activity ::onNewIntent。覆盖该方法,传递给该函数的意图应该是具有适当附加功能的新意图,等等。
归功于这个帮助我解决问题的问题:为什么我的可搜索活动的 Intent.getAction() 为 null?
This could be due to Activity::getIntent returning the Activity's original intent given certain intent flags/filters.
If that is the case for you, you'll need to look at Activity::onNewIntent. Override that method, and the intent passed to that function should be the new intent with proper extras, etc.
Credit goes to this SO question that helped me to solve my problem: Why is my searchable activity's Intent.getAction() null?