取消PendingIntent

Rog*_*pid 14 android android-pendingintent

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

views.setOnClickPendingIntent( viewId,
                PendingIntent.getBroadcast( context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT ) );
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*ght 30

如果要取消它,可以执行以下操作(代码库中的其他位置):

PendingIntent.getBroadcast(context, 0, intent, 
                           PendingIntent.FLAG_UPDATE_CURRENT).cancel();
Run Code Online (Sandbox Code Playgroud)

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

  • getBroadcast返回相同的对象*引用*以防我使用此标志? (2认同)
  • 来自 https://developer.android.com/reference/android/app/PendingIntent 人们常犯的一个错误是创建多个 PendingIntent 对象,其 Intents 仅在“额外”内容上有所不同,期望每次都能获得不同的 PendingIntent。这不会发生。Intent 中用于匹配的部分与 Intent#filterEquals(Intent) 定义的部分相同 (2认同)