从状态栏中删除通知图标

Pin*_*nki 11 android

我在状态栏中显示一个图标.现在我想在打开该内容时立即删除该图标,一段时间后如果我们收到任何提醒,该图标将再次显示.我怎样才能做到这一点?

Vid*_*nes 36

使用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);
Run Code Online (Sandbox Code Playgroud)

示例代码不完整.这取决于您创建通知的方式.只需确保使用相同的ID取消您在创建通知时使用的通知.

取消:

mNotificationManager.cancel(MY_NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)


Ond*_*nik 15

如果要在用户单击时删除通知,在创建通知之前设置通知标志FLAG_AUTO_CANCEL.