我正在开发一个应用程序为电台流媒体使用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中声明了它。
有什么需要帮忙的吗?
发布于 2020-03-10 18:43:32
查看此答案How to kill a foreground service along with the application和示例https://androidwave.com/foreground-service-android-example/
在android 8和更高版本中,你必须使用前台服务。
发布于 2020-03-11 11:47:39
在清除通知之后和终止应用程序之前添加一个小延迟,或者创建一个通知清除的函数
https://stackoverflow.com/questions/60622902
复制相似问题