是否更新通知删除服务的前台状态? [英] Does updating a Notification remove a Service's foreground status?

查看:136
本文介绍了是否更新通知删除服务的前台状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我把我的服务在前台为prevent从被杀害的使用:

In my app, I place my Service in the foreground to prevent it from being killed by using:

startForeground(NOTIFY_ID, notification);

这也显示了通知给用户(这是伟大的)。问题是,后面予需要更新通知。所以,我用的是code:

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?

这个答案,的 CommonsWare 表明,这种行为是可能的,但他不知道。因此,没有人知道实际的答案?

In this answer, CommonsWare indicates that this behavior is possible, but he's not sure. So does anyone know the actual answer?

注:我知道一个简单的方法来摆脱这种问题是重复调用 startForeground()我每次要更新通知。我看了就知道这种替代是否还会正常工作。

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.

推荐答案

的<一个href="http://developer.android.com/resources/samples/RandomMusicPlayer/index.html">RandomMusicPlayer在Android开发者网站上的应用程序使用NotificationManager更新前台服务的通知,因此机会是pretty的好,它保留了前台状态。

The RandomMusicPlayer 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.

(见setUpAsForeground()和updateNotification()在MusicService.java类。)

(See setUpAsForeground() and updateNotification() in the MusicService.java class.)

据我所知,如果取消通知的服务将不再是一个前台的服务,所以记住这一点;如果取消的通知,你就需要调用startForeground()再次恢复服务的前台状态。

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.

这篇关于是否更新通知删除服务的前台状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆