【问题标题】:How to Dismiss/Cancel the status bar notification in android programmatically如何以编程方式关闭/取消android中的状态栏通知
【发布时间】:2013-02-12 14:55:37
【问题描述】:

我使用下面给出的带有特定 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);

        }

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

我怎样才能做到这一点?

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    你可以用这个:

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

    【讨论】:

      【解决方案2】:

      全部取消

      myNotificationManager.cancelAll();

      【讨论】:

      • 这就是我想要的:)
      【解决方案3】:

      使用与通知时相同的 ID

       myNotificationManager.cancel(NOTIFICATION_ID)
      

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 2013-10-16
        • 1970-01-01
        • 2021-08-06
        • 2015-09-10
        • 1970-01-01
        相关资源
        最近更新 更多