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

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

内容简介:翻译自: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-8 / 48.00元

《国家窃听》以轻松而略带调侃的“冷幽默”风格,讲述了美国情报监视帝国大量不为人知的故事。本书以严谨而专业的视角,将“斯诺登事件”放在21世纪以来美国“全球反恐战争”以及美国情报界几十年发展的大背景下进行考察,揭示出这一事件的内在逻辑和历史必然。作者前期搜集、筛选、整理的一手素材在故事叙述过程中清晰而多层次地呈现,令本书堪称一部非虚构的美国情报界演义。一起来看看 《国家窃听》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具