Android前台服务通知替换
如果我使用 startForeground(myID, not) 启动一项服务,我可以在不停止前台进程的情况下将 not 替换为另一个服务吗?我的意思是,我有一个服务将在服务结束之前连续执行两项任务,并且我想在第一个任务结束时通知用户而不需要停止前台服务?
另外,是否可以在没有通知的情况下启动前台服务(即NULL)。
我可以使用什么策略来代替?谢谢。
if I start a service with startForeground(myID, not), can I replace the not for another one without stopping the foreground process? I mean, I have a service that will do two task in a row before service ends, and I want to notify user when the first ends without the need to stop foreground Service?
Also, is it possible to start a foreground service with no notification (i.e. NULL).
any stretegy I can use instead of? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您问题的一般答案是使用NotificationManager.notify() 方法,并在启动服务时使用传递到startForground() 方法中的相同id。
另外值得注意的是:
地位。
通知管理器.通知。相反,您必须调用 stopForeground()
这也会删除服务的前台状态。
参考Luca给出的答案和CommonsWare答案下的讨论,以及Lorne Laliberte和dmmh给出的答案。
如何更新通知Android 中前台服务的文本?
更新通知是否会删除服务的前台状态?
The general answer to your question is to use the NotificationManager.notify() method using the same id passed into the startForground() method when starting the service.
Also worth noting :
status.
NotificationManager.notify. Instead you must call stopForeground()
which removes the service's foreground status as well.
Refer to the answer given by Luca and the discussion under CommonsWare's answer, as well as the answers given in by Lorne Laliberte and dmmh.
How do I update the notification text for a foreground service in Android?
Does updating a Notification remove a Service's foreground status?
在
NotificationManager
中有一个方法cancel(int)
接收 notificationId,因此您可以取消旧的通知程序并创建一个新的通知程序。In
NotificationManager
there is methodcancel(int)
that receive notificationId, so you can just cancel old notifier and create a new one.