如果我使用 startForeground(myID, not) 启动服务,我可以在不停止前台进程的情况下将 not 替换为另一个服务吗?我的意思是,我有一个服务会在服务结束前连续执行两个任务,我想在第一个任务结束时通知用户而不需要停止前台服务?
此外,是否可以在没有通知(即 NULL)的情况下启动前台服务。
有什么策略可以代替吗?谢谢。
最佳答案
您的问题的一般答案是使用 NotificationManager.notify() 方法,使用在启动服务时传递给 startForground() 方法的相同 id。
同样值得注意的是:
- 更新通知不会删除服务的前台 状态。
- 您不能使用 NotificationManager.通知。相反,您必须调用 stopForeground() 这也删除了服务的前台状态。
引用Luca给出的答案和CommonsWare答案下的讨论,以及Lorne Laliberte和dmmh给出的答案。
How do I update the notification text for a foreground service in Android?
Does updating a Notification remove a Service's foreground status?
关于Android 前台服务通知替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519911/