对于 11 以下的 API,您可以设置 Notification.FLAG_NO_CLEAR。这可以这样实现:
// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example ", System.currentTimeMillis());
// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);
// THIS LINE IS THE IMPORTANT ONE
// This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;
对于 11 以上的 API 级别,或者使用 Android 支持库时,可以这样实现:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.yourIcon)
.setLargeIcon(R.drawable.yourBigIcon)
.setOngoing(true) // Again, THIS is the important line. This method lets the notification to stay.
.build();
或者你可以使用 NotificationCompat 类。
public NotificationCompat.Builder setAutoCancel (boolean autoCancel)
设置此标志将使通知在用户在面板中单击时自动取消。用setDeleteIntent(PendingIntent)设置的PendingIntent会在通知取消时广播。