假设您希望显示带有前台服务标志的通知,并使用nm
显示它:
Notification notification = new Notification(...);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE
nm.notify(SOME_ID, notification);
将显示通知。但是,删除它很困难,因为这似乎没有做任何事情:
nm.cancel(SOME_ID);
如何取消此通知? stopForeground不是解决方案,因为我不在服务中。
答案 0 :(得分:2)
有什么解决方案吗?
如果要相信this issue,那么除了进行服务电话stopForeground()
之外,不会。您可以通过startService()
向您的服务发送命令,以便拨打stopForeground()
。或者,您可以创建一个小小的专用Service
,只需从stopForeground()
调用stopSelf()
和onStartCommand()
,就可以删除此通知。
答案 1 :(得分:0)
解决方案在移除FLAG_FOREGROUND_SERVICE并仅使用0或FLAG_ONGOING_EVENT后,通知可以分别取消或取消取消。 对我有用,我发现这个答案很有帮助 Switching Android notification from FLAG_ONGOING_EVENT to cancelable