Android前台服务通知替换

发布于 2024-12-21 20:20:14 字数 199 浏览 2 评论 0原文

如果我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-12-28 20:20:14

您问题的一般答案是使用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 :

  • Updating the notification will NOT remove the service's foreground
    status.
  • You cannot cancel the notification using the
    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?

疯狂的代价 2024-12-28 20:20:14

NotificationManager 中有一个方法 cancel(int) 接收 notificationId,因此您可以取消旧的通知程序并创建一个新的通知程序。

In NotificationManager there is method cancel(int) that receive notificationId, so you can just cancel old notifier and create a new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文