如何在android中更新和删除Pending Intent和Notification

时间:2017-04-01 16:57:10

标签: java android

每当我尝试更新通知或Intent时,都会创建一个我不想要的新通知。我认为问题在于,每当我尝试更新时,都没有识别出意图。代码链接如下:

https://gist.github.com/Kerron-Hutton/03799ac497128919225bbbd4efaffa25

1 个答案:

答案 0 :(得分:2)

  1. 要更新PendingIntent,您应该使用之前用过的REQUEST_CODE来创建它。

    PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
  2. 要取消/删除现有PendingIntent,您可以使用PendingIntent.cancel()方法。

    PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 
                       PendingIntent.FLAG_UPDATE_CURRENT).cancel();
    
  3. 要更新Notification,您可以对要更新的NotificationManager.notify()使用NOTIFICATION_ID方法。

    NotificationManager notificationManager= (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);    
    notificationManager.notify(NOTIFICATION_ID, notification);
    
      

    通知id在您的应用中应该是唯一的。如果一个   您的个人已发布具有相同id的通知   申请并没有被取消,它将被取代   更新的信息。

  4. 要取消/删除现有Notification,请将NotificationManager.Cancel()方法与要取消的NOTIFICATION_ID一起使用。

    notificationManager.cancel(NOTIFICATION_ID);
    
  5. 希望这会有所帮助〜