Android 从状态栏中删除通知图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2839727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the notification icon from the status bar
提问by Pinki
I am displaying an icon in status bar.Now I want to remove that icon immediately when I open that content, after some time if we receive any alert, that icon will be displayed again. How can I do this?
我在状态栏中显示一个图标。现在我想在打开该内容时立即删除该图标,一段时间后如果我们收到任何警报,该图标将再次显示。我怎样才能做到这一点?
回答by Vidar Vestnes
Use the NotificationManager to cancel your notification. You only need to provide your notification id.
使用 NotificationManager 取消您的通知。您只需要提供您的通知 ID。
https://developer.android.com/reference/android/app/NotificationManager.html
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);
The example code is not complete. It depends on how your created your notification. Just make sure you use the same id to cancel your notification as you used when you created your notification.
示例代码不完整。这取决于您如何创建通知。只需确保您使用与创建通知时使用的 ID 相同的 ID 来取消通知。
To cancel:
取消:
mNotificationManager.cancel(MY_NOTIFICATION_ID);
回答by Ondra Zahradnik
If you want to remove the notification once user clicked on it, set notification flag FLAG_AUTO_CANCELbefore you create the notification.
如果您想在用户单击后删除通知,请在创建通知之前设置通知标志FLAG_AUTO_CANCEL。
回答by occasl
I used the Builder patter so you can just set auto cancel from the setter setAutoCancel(true)
. That looks something like this:
我使用了 Builder 模式,所以你可以从 setter 设置自动取消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());
回答by Guangqiu Jiang
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);