取消 PendingIntent [英] Cancelling a PendingIntent

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

问题描述

AppWidgetProvider 中使用 PendingIntent 时,我使用以下代码:

When using a PendingIntent in an AppWidgetProvider, I'm using the following code:

views.setOnClickPendingIntent( viewId,
                PendingIntent.getBroadcast( context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT ) );

所以目前没有保留对 getBroadcast 方法返回的 PendingIntent 的引用.在特定情况下,我现在想取消 PendingIntent.有没有办法从视图中取回 PendingIntent?或者是之后通过保持对它的引用来调用 PendingIntent 的取消方法的唯一方法?

So currently there's no reference kept to the PendingIntent returned by the getBroadcast method. In a specific situation I now want to cancel the PendingIntent. Is there any way of getting the PendingIntent back from the View? Or is the only way to call the cancel method of the PendingIntent afterwards by keeping a reference to it?

推荐答案

在您想要取消它的地方,您可以执行以下操作(在您的代码库中的其他地方):

Where you want to cancel it, you would do the following (somewhere else in your code base):

PendingIntent.getBroadcast(context, 0, intent, 
                           PendingIntent.FLAG_UPDATE_CURRENT).cancel();

其中 intent 与上面代码中引用的相同.PendingIntent.getBroadcast(...) 使用 PendingIntent.FLAG_UPDATE_CURRENT 将返回对已创建的现有引用的引用,如果当前不存在则创建一个引用.

where intent is the same one as referenced in your code above. PendingIntent.getBroadcast(...) using the PendingIntent.FLAG_UPDATE_CURRENT will return a reference to the existing one already created, or create one if it doesn't currently exist.

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

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