阻止 Android 终止暂停的音频服务
Prevent Android from Killing Paused Audio Service
自 Android API 26 起,Android 系统要求 "performs some operation that is noticeable to the user" 将自身注册为前台服务的服务。并且 "foreground services must display a Notification."
我有一个播放音频文件的服务。播放暂停时,用户应该能够将通知滑开(与 Google Play Music 应用程序一样)。但是如果我调用 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)
,这是默认设置),以反映您的暂停状态
第 3 步:理想情况下,调用 stopSelf()
,使服务超出内存并将进程重要性降低到正常水平,而不是等待 Android 停止服务
自 Android API 26 起,Android 系统要求 "performs some operation that is noticeable to the user" 将自身注册为前台服务的服务。并且 "foreground services must display a Notification."
我有一个播放音频文件的服务。播放暂停时,用户应该能够将通知滑开(与 Google Play Music 应用程序一样)。但是如果我调用 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)
,这是默认设置),以反映您的暂停状态
第 3 步:理想情况下,调用 stopSelf()
,使服务超出内存并将进程重要性降低到正常水平,而不是等待 Android 停止服务