35

特定の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);

        }

今私が欲しいのは、プログラムでアプリケーションのボタンをクリックしたときにこの通知を削除することです。

どうすればこれを達成できますか?

4

3 に答える 3

72

あなたはこれを使うことができます:

public void clearNotification() {
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}
于 2013-02-12T14:57:43.370 に答える
17

すべてキャンセルするには

myNotificationManager.cancelAll();

于 2014-02-18T12:17:02.993 に答える
6

通知時に使用したものと同じIDを使用してください

 myNotificationManager.cancel(NOTIFICATION_ID)
于 2013-02-12T14:57:15.400 に答える