问题描述
安卓:我试图安装一个包后,从通知栏取消通知。
什么我做的是以下内容:
Android: I am trying to cancel a notification from the notification bar after a package being installed.What I am doing is the following:
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();
和它的作品。我做错了吗?
But the notificationhelper._completeNotificationManager.cancel(id)
does not work. I tried to use notificationhelper._completeNotificationManager.cancelAll();
and it works. What I am doing wrong?
推荐答案
在我的经验,则无法取消与特定ID的所有通知,无论标签。
In my experience, you can't cancel all notifications with a particular ID, regardless of tag.
也就是说,如果你创建了这样两则通知:
That is, if you create two notifications like so:
notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
然后, notificationManager.cancel(SAME_ID)
不会取消其中任何一个!我怀疑这是因为标签字段,如果未指定的通知()和取消(),默认为空,你必须明确取消。
Then, notificationManager.cancel(SAME_ID)
won't cancel either of them! I suspect that this is because the "tag" field, if unspecified in notify() and cancel(), defaults to null, which you have to cancel explicitly.
所以,取消这两个通知,你必须调用:
So, to cancel these two notifications, you have to call:
notificationManager.cancel(TAG_ONE, SAME_ID);
notificationManager.cancel(TAG_TWO, SAME_ID);
在你的情况,你提供TEXT的标签,但使用的ID,默认使用标签= NULL取消而已。
In your case, you're supplying "TEXT" as the tag but cancelling just using the id, which defaults to using tag=null.
所以,要么不提供文字作为标记:
So, either don't provide TEXT as your tag:
_completeNotificationManager.notify(id, notification);
或者,如果你需要单独通知,不希望他们揍对方,跟踪有源标签:
Or, if you need separate notifications and don't want them to clobber each other, keep track of the active tags:
_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);
...
for (String activeTag : collectionOfActiveTags)
notificationhelper._completeNotificationManager.cancel(activeTag, id);
我希望你正在试图做什么的支持,因为它似乎是应该的。
I wish that what you're trying to do was supported, as it seems that it should be.
这篇关于NotificationManager.cancel(ID)是不是一个广播接收机内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!