Android Service.startForeground does NOT respect n

2019-02-10 03:12发布

问题:

Service.startForeground() vs NotificationManager.notify(), give different behaviors.

When using notify with two different notification ids, 2 notifications are created - Good.

When doing the same with startForground, one notification overrides the other - Bad.

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?

回答1:

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.

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).