【问题标题】:Clear exo player notification when app stops from recent apps in android studio当应用从 android studio 中的最近应用停止时清除 exo 播放器通知
【发布时间】:2020-03-11 19:47:39
【问题描述】:

我正在使用 exoplayer 库开发一个用于广播流媒体的应用程序。所以,当我关闭应用程序(从最近的应用程序等)时,我想清除(删除)exoplayer 通知栏(带有播放/停止按钮)。现在,我关闭了应用程序,但通知栏仍然出现,并且不会通过向左/向右滑动来关闭。

我尝试了以下源代码但它不起作用(我尝试将源代码的行与方法分开运行但我无法解决问题)。

public class RadioService extends Service {
......
.......
......
public void onTaskRemoved(Intent rootIntent) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(NOTIFICATION_ID);
        stopForeground(true);
        notificationManager.cancelAll();

    }

    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(NOTIFICATION_ID);

    }
}

我也在 AndroidManifest.xml 中声明了它。

有什么帮助吗?

【问题讨论】:

    标签: android-studio android-notifications exoplayer android-notification-bar ondestroy


    【解决方案1】:

    检查这个答案How to kill a foreground service along with the application 和一个例子 https://androidwave.com/foreground-service-android-example/

    在 android 8 及以上版本你必须使用前台服务。

    【讨论】:

      【解决方案2】:

      在清除通知之后和终止应用程序之前添加一个小延迟,或创建一个通知您通知已清除的函数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多