【问题标题】:Android foreground service notification replaceAndroid前台服务通知替换
【发布时间】:2012-01-21 03:19:14
【问题描述】:

如果我使用 startForeground(myID, not) 启动服务,我可以在不停止前台进程的情况下将 not 替换为另一个服务吗?我的意思是,我有一个服务会在服务结束前连续执行两个任务,我想在第一个结束时通知用户而不需要停止前台服务?

另外,是否可以在没有通知的情况下启动前台服务(即 NULL)。

我可以使用什么策略来代替?谢谢。

【问题讨论】:

  • 可以使用 NULL 通知启动前台服务,但这可能无法解决您的问题。

标签: android service notifications foreground


【解决方案1】:

您的问题的一般答案是使用 NotificationManager.notify() 方法,并在启动服务时使用传递给 startForground() 方法的相同 id。

同样值得注意:

  • 更新通知不会删除服务的前台 状态。
  • 您无法使用 NotificationManager.notify。相反,您必须调用 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?

【讨论】:

    【解决方案2】:

    NotificationManager 中有方法cancel(int) 接收notificationId,所以你可以取消旧的通知器并创建一个新的。

    【讨论】:

    • 但问题是如果我取消传递给 startForeground(id,not) 的 ID,它是否会将服务从前台返回到正常服务....
    • 我不确定,但我认为NotificationManager.cancel() 不会以某种方式影响服务
    • @Tibor,你发现了什么?取消是否意味着将服务返回后台?这个答案here 说确实如此。
    • 这是很久以前的事了,但我记得我用了startForeground,它取代了通知,不需要取消之前的。
    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多