我在状态栏中显示一个图标。现在,当我打开该内容时,我想立即删除该图标,一段时间后如果收到任何警报,该图标将再次显示。我该怎么做?
我如何才能在打开内容时立即删除状态栏中的图标,在接收到警报后再次显示它?我在状态栏中显示一个图标。现在,当我打开该内容时,我想立即删除该图标,一段时间后如果收到任何警报,该图标将再次显示。我该怎么做?
我如何才能在打开内容时立即删除状态栏中的图标,在接收到警报后再次显示它?使用NotificationManager取消您的通知。您只需要提供通知id即可。
https://developer.android.com/reference/android/app/NotificationManager.html
private static final int MY_NOTIFICATION_ID= 1234;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);
这个示例代码不完整,它取决于你如何创建通知。只需确保在取消通知时使用与创建通知时相同的ID。
要取消:
mNotificationManager.cancel(MY_NOTIFICATION_ID);
setAutoCancel(true)
来设置自动取消。代码如下所示: String title = "Requests";
String msg = "New requests available.";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_gcm_icon)
.setContentTitle(title)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Intent resultIntent = new Intent(application, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(application, 0, resultIntent, 0);
NotificationManager nmgr = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(application)
.setSmallIcon(R.drawable.icon_battery)
.setContentTitle(application.getString(R.string.app_name))
.setContentText("your text")
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setTicker("your ticker")
.setDefaults(Notification.DEFAULT_SOUND ) //| Notification.DEFAULT_VIBRATE
.setContentIntent(resultPendingIntent)
.setVisibility(VISIBILITY_SECRET)
.setPriority(Notification.PRIORITY_MIN);
Notification mNotification = mBuilder.build();
// mNotification.flags |= FLAG_NO_CLEAR;
nmgr.notify(0, mNotification);