NotificationManager.cancel()不为我工作 [英] NotificationManager.cancel() doesn't work for me

查看:2831
本文介绍了NotificationManager.cancel()不为我工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试了几个小时,除去使用永久通知由服务设置:

I've been trying for hours to remove a persistent Notification set by a Service using:

startForeground(1337, notification);

在code我用取消它:

The code I'm using to cancel it:

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(1337);  // cancel existing service notification, doesn't take effect
nManager.cancelAll(); //surpluous, but also doesn't take effect

我缺少的是在这里吗?我感到困惑。

What am I missing here? I'm baffled.

要澄清我为什么这样做:服务与默认持久性通知开始。当我的应用程序运行,它需要更换此通知与另一个。使用通知()上现有的通知完美的作品,但是,我需要它来显示新通知,股票代码文本。这就是为什么我决定删除现有的通知(使用上述code),创建一个新的,然后我 startForeground()再次调用并传递新通知它,所以我坚持服务

To clarify why I am doing this: the Service starts with a default persistent Notification. When my app runs, it needs to replace this Notification with another. Using notify() on the existing Notification works perfectly, however, I need it to show the ticker text for the new Notification as well. This is why I decided to remove the existing Notification (using the code above), create a new one, and then I call startForeground() again and pass the new Notification to it, so my Service persists.

推荐答案

的问题是,你正在使用发出通知以间接的方式 startForeground()。你不能只是取消通知书制度坚持你开始前台服务时提供通知一样的道理。只要你的前景服务正在运行,通知将在那里。

The problem is that you're issuing the Notification in an indirect way by using startForeground(). You can't just cancel that Notification for the same reason the system insists on you providing a Notification when starting a foreground Service. As long as your foreground Service is running, that Notification will be there.

在大多数情况下,服务真的不应该在前台。如果你可以使用普通优先级为您服务,那么你就可以正常启动和停止的通知。

In most cases, Services really shouldn't be in the foreground. If you can use a normal priority for your Service, then you can start and stop your Notification normally.

如果你实际上做的事情,真正确实需要一个前台服务,如果你真的想用户显示股票的文本,我相信你唯一的选择是发出一个通知。

If you're actually doing something that truly does require a foreground Service, and if you really want to show the user a ticker text, I believe your only option is to issue another Notification.

这篇关于NotificationManager.cancel()不为我工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆