当Android Studio中的最新应用停止运行时,清除exo播放器通知

问题描述 投票:0回答:2

我正在使用exoplayer库开发用于广播流的应用程序。因此,我想在关闭应用程序时(从最近的应用程序e.t.c)清除(删除)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
2个回答
0
投票

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

在android 8及更高版本上,您必须使用前台服务。


0
投票

在清除通知后,终止应用程序之前或创建一个可以通知您清除通知的功能之前,要稍加延迟

© www.soinside.com 2019 - 2024. All rights reserved.