更新通知是否会删除服务的前台状态?
在我的应用程序中,我将我的服务放在前台,以防止它被使用以下命令杀死:
startForeground(NOTIFY_ID, notification);
这也向用户显示通知(这很棒)。问题是稍后我需要更新通知。所以我使用代码:
notification.setLatestEventInfo(getApplicationContext(), someString, someOtherString, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
那么问题是:这样做会使服务脱离其特殊的前台状态吗?
在这个答案中,CommonsWare< /a> 表明这种行为是可能的,但他不确定。那么有人知道真正的答案吗?
注意:我知道解决这个问题的一个简单方法是每次我想更新通知时重复调用 startForeground()
。我想知道这个替代方案是否也有效。
In my app, I place my Service in the foreground to prevent it from being killed by using:
startForeground(NOTIFY_ID, notification);
This also displays the notification to the user (which is great). The problem is that later I need to update the notification. So I use the code:
notification.setLatestEventInfo(getApplicationContext(), someString, someOtherString, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
The question then is: will doing this knock the Service out of it's special foreground status?
In this answer, CommonsWare indicates that this behavior is possible, but he's not sure. So does anyone know the actual answer?
Note: I am aware that a simple way to get out of this question is to repeatedly call startForeground()
every time I want to update the notification. I'm looking to know whether this alternative will also work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了澄清这里所说的内容:
答案的这部分建议可以通过在持久
Service
上使用NotificationManager.cancel()
来删除由Service
设置的正在进行的Notification
代码>通知。这不是真的。
无法使用
NotificationManager.cancel()
删除由startForeground()
设置的持续通知。删除它的唯一方法是调用
stopForeground(true)
,因此正在进行的通知被删除,这当然也会使Service
停止在前台。所以实际上是相反的;Service
不会因为Notification
被取消而停止在前台,Notification
只能通过停止Service 来取消
位于前台。当然,人们可以在此之后立即调用
startForeground()
,以使用新的Notification
恢复状态。如果必须再次显示股票行情文本,您需要执行此操作的一个原因是,它只会在第一次显示通知
时运行。此行为没有记录,我浪费了 4 个小时试图找出无法删除
通知
的原因。有关此问题的更多信息,请参见:NotificationManager.cancel() 对我不起作用
To clarify what has been said here:
This part of the answer suggest it is possible to remove an ongoing
Notification
set by aService
by usingNotificationManager.cancel()
on the persistentNotification
.This is not true.
It's impossible to remove a ongoing notification set by
startForeground()
by usingNotificationManager.cancel()
.The only way to remove it, is to call
stopForeground(true)
, so the ongoing Notification is removed, which ofcourse also makes theService
stop being in the foreground. So it's actually the other way around; theService
doesn't stop being in the foreground because theNotification
is cancelled, theNotification
can only be cancelled by stopping theService
being in the foreground.Naturally one could call
startForeground()
after this right away, to restore the state with a newNotification
. One reason you would want to do this if a ticker text has to be shown again, because it will only run the first time theNotification
is displayed.This behaviour is not documented, and I wasted 4 hours trying to figure out why I couldn't remove the
Notification
.More on the issue here: NotificationManager.cancel() doesn't work for me
RandomMusicPlayer (已存档) 应用程序使用NotificationManager 来更新前台服务的通知,因此它很有可能保留前台状态。
(参见 setUpAsForeground() 和 updateNotification()< /a> 在 MusicService.java 类中。)
据我了解,如果您取消通知,该服务将不再是前台服务,因此请记住这一点;如果取消通知,则需要再次调用 startForeground() 来恢复服务的前台状态。
The RandomMusicPlayer (archived) app at the Android developer site uses NotificationManager to update the notification of a foreground service, so chances are pretty good that it retains the foreground status.
(See setUpAsForeground() and updateNotification() in the MusicService.java class.)
From what I understand, if you cancel the notification the service will cease being a foreground service, so keep that in mind; if you cancel the notification, you'll need to call startForeground() again to restore the service's foreground status.
当你想更新startForeground()设置的Notification时,只需构建一个新的notification,然后使用NotificationManager来通知它。
关键点是使用相同的通知ID。
更新通知不会将服务从前台状态中删除(这只能通过调用 stopForground 来完成);
例子:
When you want to update a Notification set by startForeground(), simply build a new notication and then use NotificationManager to notify it.
The key point is to use the same notification id.
Updating the Notification will NOT remove the Service from the foreground status (this can be done only by calling stopForground );
Example: