Android 9:后台限制应用设置 [英] Android 9: Background Restriction App Setting

查看:72
本文介绍了Android 9:后台限制应用设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个音乐播放器应用程序,当用户启用后台限制"设置(设置 -> 应用程序 -> [应用程序名称] -> 电池 -> 背景)时,我注意到 Android 9 设备上的奇怪行为限制).

I'm working on a music player app and I'm noticing weird behavior on Android 9 devices when a user enables the "Background Restriction" setting (Settings -> Apps -> [App Name] -> Battery -> Background Restriction).

这是我正在做的事情:我通过调用 Service.startService() 来启动我的音乐播放器服务,然后在我的应用程序处于前台时通过 Service.startForeground() 将其设置为前台.

Here's what I'm doing: I start my music player service by calling Service.startService() then set it to foreground via Service.startForeground() while my app is in the foreground.

这是我在打开后台限制"时看到的内容:1) Service.startForeground() 不会发布通知2)我的前台服务在我的应用程序进入后台后一分钟内被操作系统杀死

Here's what I'm seeing when "Background Restriction" is turned on: 1) Service.startForeground() will not posting a notification 2) My foreground service is killed by the OS within a minute of my app going to the background

这是我在日志中看到的:1) 调用 Service.startForeground() 时出现Service.startForeground() not allowed due to bg 限制"2) 当我的应用程序被操作系统自动终止时,由于应用程序空闲而停止服务"

Here's what I see in the logs: 1) "Service.startForeground() not allowed due to bg restriction" when calling Service.startForeground() 2) "Stopping service due to app idle" when my app is auto-killed by the OS

这是我的问题:我认为前台服务的全部意义在于允许在用户知情的情况下进行后台处理(持续通知);后台限制"设置真的是为了禁止所有后台活动吗?

Here's my question: I thought the whole point of a foreground service is to allow background processing with the user's knowledge (an ongoing notification); is the "Background Restriction" setting really intended to disallow all background activity?

有趣的发现:在 GitHub 上查看 Google 的通用音乐播放器"示例项目,我注意到他们的示例项目并没有像我的应用程序那样被杀死.挖掘后我注意到这是因为它们绑定到他们的服务并且永远不会在 Activity.onPause() 中解除绑定.根据谷歌的文档,绑定服务不受相同的后台限制.这真的是我问题的解决方案吗?看起来有点笨拙/脆弱.

Interesting find: Looking at Google's "Universal Music Player" sample project on GitHub, I noticed that their sample project is not being killed like my app is. After digging I noticed this is because they are binding to their service and never unbinding in Activity.onPause(). According to Google's docs, bound services are not subject to the same background restrictions. Is this really the solution to my problem? Seems a little hacky/fragile.

预先感谢您的帮助!

推荐答案

这是我发现的:

  • 后台限制"(或某些设备上的允许后台活动")旨在停止所有后台活动,无论您的服务是否调用了 setForeground()

  • "Background Restriction" (or "Allow Background Activity" on some devices) is intended to stop ALL background activity regardless of whether your service has called setForeground()

没有办法绕过这个设置.您不能以编程方式禁用它.您唯一的选择是使用 ActivityManager.isBackgroundRestricted() 以编程方式检查它是否已启用并显示一个弹出窗口,通知您的用户如何禁用此设置

There is no way around this setting. You cannot programmatically disable it. Your only option is to programmatically check if it's enabled using ActivityManager.isBackgroundRestricted() and display a pop-up informing your users on how to disable this setting

Google 的 GitHub 上的通用音乐播放器示例项目恰好可以工作(截至撰写本答案时),只是因为在主 Activity 暂停时未释放服务绑定.然而,当主 Activity 被垃圾收集时(通常为 30-45 分钟,具体取决于设备),示例项目的服务被终止.

Google's Universal Music Player sample project on GitHub happens to work (as of the writing of this answer) only because a service bind is not released when the main Activity is paused. The sample project's service is however killed when the main Activity is garbage collected (typically 30-45 minutes depending on the device).

这篇关于Android 9:后台限制应用设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆