在 AppWidgetProvider
中使用 PendingIntent 时,我使用了以下代码:
views.setOnClickPendingIntent( viewId,
PendingIntent.getBroadcast( context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT ) );
因此,目前没有保留对 getBroadcast 方法返回的 PendingIntent 的引用。在特定情况下,我现在想取消 PendingIntent。有没有办法从 View 中取回 PendingIntent?还是通过保留对 PendingIntent 的引用来调用 PendingIntent 的取消方法的唯一方法?
最佳答案
在您想取消它的地方,您可以执行以下操作(在您的代码库中的其他地方):
PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT).cancel();
其中 intent
与上面代码中引用的相同。 PendingIntent.getBroadcast(...)
使用 PendingIntent.FLAG_UPDATE_CURRENT
将返回对已创建的现有广播的引用,如果当前不存在则创建一个。
关于android - 取消 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823408/