android – NotificationManager.cancel(id)在广播接收器中不起作用

栏目: IOS · Android · 发布时间: 7年前

内容简介:翻译自: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)在广播接收器中不起作用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

构建之法(第二版)

构建之法(第二版)

邹欣 / 人民邮电出版社 / 2015-7 / 59

软件工程牵涉的范围很广, 同时也是一般院校的同学反映比较空洞乏味的课程。 但是软件工程的技术对于投身IT 产业的学生来说是非常重要的。作者邹欣有长达20年的一线软件开发经验,他利用业余时间在数所高校进行了长达6年的软件工程教学实践,总结出了在16周的时间内让同学们通过 “做中学 (Learning By Doing)” 掌握实用的软件工程技术的教学计划,并得到高校师生的积极反馈。在此基础上,作者对......一起来看看 《构建之法(第二版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具