如何获取和取消 PendingIntent? [英] How to get and cancel a PendingIntent?

查看:33
本文介绍了如何获取和取消 PendingIntent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 alarmManager,用于在特定时间向用户发送通知.由于有多个警报,我正在创建多个待处理意图并提供唯一 ID,但是在某些情况下,我需要获取所有待处理意图然后取消它们,以便我可以重置警报.我已经尝试过这样做,但似乎仍然无法正确解决,所以我有几个问题:

I have an alarmManager which I am using to send notifications to the user at specific times. Since there are multiple alarms, I have multiple pending intents that I am creating and giving a unique ID, However there are certain situations in which I will need to get all the pending intents and then cancel them, so I can reset the alarms. I have tried doing this and I still cant seem to get it right so I have a couple questions:

这是您正确获取和取消 PendingIntent 的方式吗?

Is this how you would correctly get and cancel a PendingIntent?

Intent intent = new Intent(con, AppointmentNotificationReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(con, id, intent,
        PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
am.cancel(sender);

意图是否需要与原始待定意图(额外和所有)完全匹配?

Does the intent need to exactly match that of the original pending intent(extras and all)?

PendingIntent 标志是否需要与原始待处理意图的标志相匹配?

Does the PendingIntent flag need to match that of the original pending intent?

推荐答案

我发现您实际上并没有获得"待定的意图……您必须完全按照最初创建它时的样子重新创建它(意图以及),然后将其传递给 AlarmManager 的取消函数.所以我发布的上面的代码真的没有错,只要我第一次创建它的方式就行.希望有人会发现这有帮助.

I found out that you do not actually "get" the pending intent...you have to recreate it exactly as it was when you first created it(Intent as well) and then pass it to the AlarmManager's cancel function. So the above code I posted really is not incorrect as long as thats how I first created it. Hopefully someone will find this helpful.

这篇关于如何获取和取消 PendingIntent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆