内容简介:翻译自:https://stackoverflow.com/questions/13062798/notificationmanager-cancelid-is-not-working-inside-a-broadcast-receiver
Android:我正在尝试在安装软件包后取消通知栏中的通知.
我正在做的是以下内容:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Uri data = intent.getData();
//some code goes here
//get the id of the notification to cancel in some way
notificationhelper._completeNotificationManager.cancel(id);
}
}
}
哪里
public class notificationhelper {
public static NotificationManager _completeNotificationManager = null;
public void complete() {
if (_completeNotificationManager == null)
_completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.notification,
_context.getString(R.string.notification),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_NO_CLEAR;
_completeNotificationManager.notify(TEXT, id, notification);
}
}
但是notificationhelper._completeNotificationManager.cancel(id)不起作用.我试着使用notificationhelper._completeNotificationManager.cancelAll();它的工作原理.我做错了什么?
根据我的经验,无论标签如何,您都无法取消具有特定ID的所有通知.
也就是说,如果您创建两个通知,如下所示:
notificationManager.notify(TAG_ONE, SAME_ID, notification_one); notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
然后,notificationManager.cancel(SAME_ID)将不会取消它们中的任何一个!我怀疑这是因为“tag”字段,如果在notify()和cancel()中未指定,则默认为null,您必须明确取消.
因此,要取消这两个通知,您必须致电:
notificationManager.cancel(TAG_ONE, SAME_ID); notificationManager.cancel(TAG_TWO, SAME_ID);
在您的情况下,您提供“TEXT”作为标记,但仅使用id取消,默认使用tag = null.
因此,要么不提供TEXT作为您的标记:
_completeNotificationManager.notify(id, notification);
或者,如果您需要单独的通知并且不希望它们互相破坏,请跟踪活动标记:
_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);
...
for (String activeTag : collectionOfActiveTags)
notificationhelper._completeNotificationManager.cancel(activeTag, id);
我希望你正在尝试做的事情得到支持,因为它似乎应该是这样.
翻译自:https://stackoverflow.com/questions/13062798/notificationmanager-cancelid-is-not-working-inside-a-broadcast-receiver
以上所述就是小编给大家介绍的《android – NotificationManager.cancel(id)在广播接收器中不起作用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 关于在接收POST请求,Tomcat偶发性接收到的参数不全问题排查分析
- 异步接收MSMQ消息
- 如何突破商品期货Tick接收限制
- SpringMVC接收和响应json数据
- 如何使用 jq 接收 blob 数据
- 转的关于公众号接收信息的返回
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。