【问题标题】:Cancelling a PendingIntent with unique requestCode使用唯一的 requestCode 取消 PendingIntent
【发布时间】:2013-10-18 22:04:52
【问题描述】:

我无法取消我的未决意向。现在我像这样创建我的待处理意图:

Intent intent = new Intent(_self, NotificationService.class); PendingIntent pi = PendingIntent.getService(this, task.getID, intent, 0);

task.getID 在这里是一个唯一的 int。稍后在我的应用程序中,用户可以编辑“任务”并决定取消通知(待定)。我尝试像这样取消pendingintent:

Intent intent = new Intent(_self, NotificationService.class); PendingIntent pIntent = PendingIntent.getBroadcast(_self, task.getID, intent , PendingIntent.FLAG_NO_CREATE);
pIntent.cancel();

但它不起作用!通知仍然显示。在这种情况下,task.getID 指的是之前创建的唯一 id。

我尝试了几个不同的参数,PendingIntent.FLAG_NO_CREATE 返回 null,FLAG_UPDATE_CURRENT 不起作用,FLAG_CANCEL_CURRENT 也没有。有人可以帮我看看我做错了什么吗?

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    您正在尝试使用 PendingIntent 创建服务并尝试取消广播。它永远不会起作用。

    您需要使用您尝试取消的 PendingIntent 来获取服务。

    【讨论】:

      【解决方案2】:

      当您想取消它时,您需要以与创建第一个意图完全相同的方式重新创建待处理的意图。

      看看:Cancelling a PendingIntent

      【讨论】:

      • 我确实以完全相同的方式重新创建它。不管我是否添加额外的,它仍然不起作用:(
      猜你喜欢
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2023-04-08
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      相关资源
      最近更新 更多