Android Service.startForeground 不尊重通知 ID 的唯一性 [英] Android Service.startForeground does NOT respect notification id uniqueness
问题描述
Service.startForeground() vs NotificationManager.notify(),给出不同的行为.
Service.startForeground() vs NotificationManager.notify(), give different behaviors.
将 notify 与两个不同的通知 ID 一起使用时,会创建 2 个通知 - 好.
When using notify with two different notification ids, 2 notifications are created - Good.
当对 startForground 做同样的事情时,一个通知会覆盖另一个 - 不好.
When doing the same with startForground, one notification overrides the other - Bad.
测试设备:Nexus S(2.3.6) 和 Asus Transformer (4.0.3).
Tested device: Nexus S(2.3.6) and Asus Transformer (4.0.3).
有什么想法可以运行一个可以有多个(动态数量)通知的重要(前台)服务吗?
Any ideas how I can run an Important (foreground) service that can have several (dynamic number) notifications?
推荐答案
好吧,这不是最好的解决方案,但您始终可以将通知重新分配"给已取消的通知 ID.例如,假设您启动进程 1(前台),然后启动进程 2(使用 NotificationManager).当流程 1 结束时,取消流程 2 的通知并更新流程 1 的前台通知,以实际显示流程 2 的进度.
Well, it's not the best solution, but you can always just "reassign" a notification to a dismissed notification ID. For example, say you start Process 1 (foreground) and then Process 2 (using NotificationManager). When Process 1 ends, dismiss the notification for Process 2 and update the foreground notification for Process 1 to actually now show the progress for Process 2.
这样你就不必调用 stopForeground()
和 startForeground()
,这可能允许服务在两者之间被杀死(实际上没有检查不过确实如此).
That way you don't have to call stopForeground()
and startForeground()
, which may allow the service to be killed in between (haven't actually checked that's true, though).
这篇关于Android Service.startForeground 不尊重通知 ID 的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!