Android 安卓-如何让通知在用户点击时消失

Android 安卓-如何让通知在用户点击时消失,android,android-layout,notificationmanager,Android,Android Layout,Notificationmanager,这是我的通知代码: NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.ic_launcher; CharSequence tickerText = "Ticker Text"; long time = System.currentTimeMillis()

这是我的通知代码:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Ticker Text";
    long time = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, time);
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    Context context = getApplicationContext();
    CharSequence contentTitle = "Title";
    CharSequence contentText = "Text";
    Intent notificationIntent = new Intent(this, FistActiivty.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(1,notification);
我如何才能让通知仅在用户单击时消失,我的意思是我不希望通知在用户单击“清除”按钮时消失


谢谢

公共静态最终整数通知\u ID=1

NotificationManager MNNotificationManager=(NotificationManager) this.getSystemService(Context.NOTIFICATION\u服务)

Intent myintent=新的Intent(这是“您的活动名称”);例:mainactivity.class

        myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);


        mBuilder.setAutoCancel(true); // this method is use for disappear notification after tap on it .
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build())

使用:

// This notification will not be cleared by swiping or by pressing "Clear all"
notification.flags |= Notification.FLAG_NO_CLEAR;
同样重要的是:

.setOngoing(true) //put this inside your notification builder
或使用:
Notification.FLAG\u持续事件而不是
。设置正在进行(true)


这应该会对您有所帮助。

您可以将其设置为持续通知。并为通知实现onClick,通过它可以清除相同的通知。如果用户点击它,它会清除通知吗?