从 Android API 26 开始,Android 系统需要一个服务 "performs some operation that is noticeable to the user"将自己注册为前台服务。并且“前台服务必须显示通知。”
我有一个播放音频文件的服务。播放暂停时,用户应该能够将通知滑开(与 Google Play 音乐应用程序一样)。但是,如果我调用 stopForeground()
,那么我的服务会在几分钟内被系统终止。
如何在播放暂停时让通知滑动消失?
最佳答案
But if I call stopForeground() then my Service is killed by the system within a few minutes
在 Android 8.0+ 上,它会在一分钟后停止。
How can I allow the notification to be swiped away when playback is paused?
第 0 步:将您真正需要的任何数据(例如,轨道和播放位置)保存到磁盘
第 1 步:调用 stopForeground()
第 2 步:发出一个新的通知,一个没有设置“ongoing”标志(即 setOngoing(false)
,这是默认设置)的通知,以反射(reflect)您的暂停状态
第 3 步:理想情况下,调用 stopSelf()
,将服务从内存中取出并将您的进程重要性降低到正常水平,而不是等待 Android 停止您的服务
关于android - 防止 Android 终止暂停的音频服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51973954/