Java android中的自动取消通知

Java android中的自动取消通知,java,android,android-notifications,Java,Android,Android Notifications,我想通过setAutoCancle()方法关闭通知。 下面是NotifactionExample的代码 @SuppressWarnings("deprecation") protected void notifactionAlert() { // TODO Auto-generated method stub NotificationManager myNotifaction = (NotificationManager) getSystemService(NOTIFICATI

我想通过
setAutoCancle()
方法关闭通知。 下面是
NotifactionExample
的代码

@SuppressWarnings("deprecation")
protected void notifactionAlert() {
    // TODO Auto-generated method stub

    NotificationManager myNotifaction = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    @SuppressWarnings("deprecation")
    Notification Notifaction = new Notification(R.drawable.notifaction,
            "Notifaction received.", System.currentTimeMillis());


    Intent notifactionIntent = new Intent(MainActivity.this, notify.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notifactionIntent, 0);
    Notifaction.setLatestEventInfo(MainActivity.this, "Notifaction.",
            "you get a new notifaction...", pendingIntent);
    myNotifaction.notify(9999, Notifaction);

}

我能做什么?

您需要做的就是打开自动取消标志

Notifaction.flags |= Notification.FLAG_AUTO_CANCEL

您只需将自动取消标志设置为On

Notifaction.flags |= Notification.FLAG_AUTO_CANCEL

单击任意按钮即可尝试:

@Override
    public void onClick(View v) {
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification noti = new Notification.Builder(this)
        .setTicker("Ticker Title")
        .setContentTitle("Content Title")
        .setContentText("Notification content.")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent).getNotification();
        noti.flags=Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti); 
    }

单击任意按钮即可尝试:

@Override
    public void onClick(View v) {
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification noti = new Notification.Builder(this)
        .setTicker("Ticker Title")
        .setContentTitle("Content Title")
        .setContentText("Notification content.")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent).getNotification();
        noti.flags=Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti); 
    }
您还可以检查,标记\u否\u清除


也可以查一下,,标记\u否\u清除

是否检查stackvoerflow上的类似问题?是否要在用户看到后或之前删除通知?我想在用户单击通知时取消通知。是否检查stackvoerflow上的类似问题?是否要在用户看到后或之前删除通知?我想取消通知当用户单击通知时。