如何closures/以编程方式取消android中的状态栏通知

我已经创build了一个状态栏通知android编程使用下面给出的代码与一个特定的ID

Notification notification; public static NotificationManager myNotificationManager; public static final int NOTIFICATION_ID = 1; private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) { myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); long when = System.currentTimeMillis(); notification = new Notification(icon, NotificationTicket, when); Intent notificationIntent = new Intent(context, MyActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0); notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent); notification.flags |= Notification.FLAG_ONGOING_EVENT; myNotificationManager.notify(NOTIFICATION_ID, notification); } 

现在我想要的是通过单击程序中的button删除此通知。

我怎么能做到这一点?

Related of "如何closures/以编程方式取消android中的状态栏通知"

你可以使用这个:

 public void clearNotification() { NotificationManager notificationManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(NOTIFICATION_ID); } 

myNotificationManager.cancelAll();

使用通知时使用的相同ID

  myNotificationManager.cancel(NOTIFICATION_ID)