如何以编程方式在android中解除/取消状态栏通知

时间:2013-02-12 14:55:38

标签: android android-notifications

我已使用下面给出的具有特定ID

的代码以编程方式在android中创建状态栏通知
    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);

        }

现在我想要的是以编程方式单击应用程序中的按钮来删除此通知。

我怎样才能实现这个目标?

3 个答案:

答案 0 :(得分:59)

您可以使用:

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

答案 1 :(得分:12)

取消所有

myNotificationManager.cancelAll();

答案 2 :(得分:5)

使用与通知时使用的相同ID

 myNotificationManager.cancel(NOTIFICATION_ID)