更新通知是否会删除服务的前台状态?

发布于 2024-12-18 02:22:57 字数 658 浏览 2 评论 0原文

在我的应用程序中,我将我的服务放在前台,以防止它被使用以下命令杀死:

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 技术交流群。

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

发布评论

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

评论(3

姐不稀罕 2024-12-25 02:22:57

为了澄清这里所说的内容:

据我了解,如果您取消通知服务
将不再是前台服务,因此请记住这一点;如果你
取消通知,您需要再次调用startForeground()
恢复服务的前台状态。

答案的这部分建议可以通过在持久 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:

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.

This part of the answer suggest it is possible to remove an ongoing Notification set by a Service by using NotificationManager.cancel() on the persistent Notification.
This is not true.
It's impossible to remove a ongoing notification set by startForeground() by using NotificationManager.cancel().

The only way to remove it, is to call stopForeground(true), so the ongoing Notification is removed, which ofcourse also makes the Service stop being in the foreground. So it's actually the other way around; the Service doesn't stop being in the foreground because the Notification is cancelled, the Notification can only be cancelled by stopping the Service being in the foreground.

Naturally one could call startForeground() after this right away, to restore the state with a new Notification. 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 the Notification 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

壹場煙雨 2024-12-25 02:22:57

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.

清晰传感 2024-12-25 02:22:57

当你想更新startForeground()设置的Notification时,只需构建一个新的notification,然后使用NotificationManager来通知它。

关键点是使用相同的通知ID。

更新通知不会将服务从前台状态中删除(这只能通过调用 stopForground 来完成);

例子:

private static final int notif_id=1;

@Override
public void onCreate (){
    this.startForeground();
}

private void startForeground() {
        startForeground(notif_id, getMyActivityNotification(""));
}

private Notification getMyActivityNotification(String text){
        // The PendingIntent to launch our activity if the user selects
        // this notification
        CharSequence title = getText(R.string.title_activity);
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                0, new Intent(this, MyActivity.class), 0);

        return new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_launcher_b3)
                .setContentIntent(contentIntent).getNotification();     
}
/**
this is the method that can be called to update the Notification
*/
private void updateNotification() {

                String text = "Some text that will update the notification";

                Notification notification = getMyActivityNotification(text);

                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(notif_id, notification);
}

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:

private static final int notif_id=1;

@Override
public void onCreate (){
    this.startForeground();
}

private void startForeground() {
        startForeground(notif_id, getMyActivityNotification(""));
}

private Notification getMyActivityNotification(String text){
        // The PendingIntent to launch our activity if the user selects
        // this notification
        CharSequence title = getText(R.string.title_activity);
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                0, new Intent(this, MyActivity.class), 0);

        return new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_launcher_b3)
                .setContentIntent(contentIntent).getNotification();     
}
/**
this is the method that can be called to update the Notification
*/
private void updateNotification() {

                String text = "Some text that will update the notification";

                Notification notification = getMyActivityNotification(text);

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