一旦服务不再处于前台,就可以取消来自前台服务的通知 [英] Make a notification from a foreground service cancelable once the service is not in foreground anymore

查看:63
本文介绍了一旦服务不再处于前台,就可以取消来自前台服务的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个音乐控制通知,允许用户开始/停止音乐.我想要与 Google Play 音乐应用程序通知完全相同的行为:播放音乐时,服务处于前台且通知不可取消,当音乐未播放时,服务不再处于前台且通知可以移除.它工作正常,但是当我取消我的服务的前台时,通知在重新出现之前很快被删除.

I have a music control notification that allows the user to start/stop the music. I want exactly the same behavior than the Google Play Music app notification: when music is playing, the service is in foreground and the notification not cancelable, and when the music is not playing the service is not in the foreground anymore and the notification can be removed. It works fine but when I cancel the foreground of my service, the notification is quickly removed before reappearing just after.

这是我的代码,首先是我如何构建通知:

Here's my code, first how I build the notification:

NotificationCompat.Builder notifBuilder =
            new android.support.v7.app.NotificationCompat.Builder(getApplicationContext())
                    .setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
                            .setShowActionsInCompactView(1, 2, 3)
                            .setShowCancelButton(true)
                            .setCancelButtonIntent(deletePendingIntent)))
                    .setSmallIcon(R.drawable.notif_logo)
                    .setColor(ResourcesCompat.getColor(getResources(), R.color.blue, getTheme()))
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setShowWhen(false);

    notifBuilder.setContentIntent(pendingIntent);
    notifBuilder.setDeleteIntent(deletePendingIntent);

这是我开始和更新通知的方式:

And here is how I start and update my notification:

private void showNotification(NotificationCompat.Builder notifBuilder, boolean foreground) {
    if (foreground) {
        startForeground(NOTIFICATION_ID, notifBuilder.build());
    } else {
        stopForeground(false);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notifBuilder.build());
    }
}

如果我使用 stopForeground(false),通知在运行后仍然无法取消.如果我使用 stopForeground(true),通知会被快速删除然后再次添加,这会导致奇怪的闪烁.

If I use stopForeground(false), the notification is still not cancelable after running it. If I use stopForeground(true), the notification is quickly removed then added again which give a weird blinking.

如何在服务退出前台后可以取消通知,而不必删除然后再次添加通知?

How can I have a notification that is cancelable after a service quit being in foreground, without having to remove then add again the notification?

推荐答案

根据 使用带有前台服务文档的 MediaStyle 通知:

在 Android 5.0(API 级别 21)及更高版本中,一旦服务不再在前台运行,您可以滑动通知以停止播放器.您不能在早期版本中执行此操作.为了让用户在Android 5.0(API级别21)之前移除通知并停止播放,您可以通过调用setShowCancelButton(true)setCancelButtonIntent().

In Android 5.0 (API level 21) and later you can swipe away a notification to stop the player once the service is no longer running in the foreground. You can't do this in earlier versions. To allow users to remove the notification and stop playback before Android 5.0 (API level 21), you can add a cancel button in the upper-right corner of the notification by calling setShowCancelButton(true) and setCancelButtonIntent().

您永远不需要调用 setOngoing(false)/setOngoing(true),因为这取决于您的服务当前是否在前台.

You never need to call setOngoing(false)/setOngoing(true) as that is controlled by whether your service is currently in the foreground.

根据 媒体会话回调文档,当您的音乐暂停时,您应该被调用 stopForeground(false) - 这会删除前台优先级并允许用户在 API 21+ 设备上滑动通知.

As per the Media Session Callbacks docs, you should be called stopForeground(false) when your music gets paused - this remove the foreground priority and allows users to swipe the notification away on API 21+ devices.

这篇关于一旦服务不再处于前台,就可以取消来自前台服务的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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