Android O 设置为应用启用后台限制
Android O Settings to enable background restriction for applications
根据 Docs-
Note: By default, these restrictions only apply to apps that target
Android 8.0 (API level 26) or higher. However, users can enable most
of these restrictions for any app from the Settings screen, even if
the app targets an API level lower than 26.
我的目标是 API 26 以下,但我找不到在官方 android 模拟器中启用限制的设置。
在哪里可以使用此设置?
启用后台限制的设置可在App Info -> 电池屏幕。
尚未在模拟器中重现。
该选项在您最初安装应用程序时不可用(目标低于 API 26),直到该应用程序执行一些后台工作。
所以我能够通过-
制作它
- 目标应用程序 API 25
- 创建一个长 运行 服务(大约 5 分钟)
- 从MainActivity的onCreate开始服务
- 将应用程序发送到后台(按返回键)并让设备保持电池供电(未连接电源)
几分钟后,该选项将可用以启用限制。
您可以使用 adb 来 enable/disable 后台限制。
Android 7.0 (API level 24) introduces some additional Android Debug Bridge (ADB) commands that you can use to test app behavior with those background processes disabled:
- To simulate conditions where implicit broadcasts and background services are unavailable, enter the following command:
$ adb shell cmd appops set <package_name> RUN_IN_BACKGROUND ignore
- To re-enable implicit broadcasts and background services, enter the following command:
$ adb shell cmd appops set <package_name> RUN_IN_BACKGROUND allow
来源:https://developer.android.com/topic/performance/background-optimization#further-optimization
您还可以在后台将您的应用设置为 运行:
adb shell am make-uid-idle <package>
根据 Docs-
Note: By default, these restrictions only apply to apps that target Android 8.0 (API level 26) or higher. However, users can enable most of these restrictions for any app from the Settings screen, even if the app targets an API level lower than 26.
我的目标是 API 26 以下,但我找不到在官方 android 模拟器中启用限制的设置。
在哪里可以使用此设置?
启用后台限制的设置可在App Info -> 电池屏幕。
尚未在模拟器中重现。
该选项在您最初安装应用程序时不可用(目标低于 API 26),直到该应用程序执行一些后台工作。
所以我能够通过-
制作它- 目标应用程序 API 25
- 创建一个长 运行 服务(大约 5 分钟)
- 从MainActivity的onCreate开始服务
- 将应用程序发送到后台(按返回键)并让设备保持电池供电(未连接电源)
几分钟后,该选项将可用以启用限制。
您可以使用 adb 来 enable/disable 后台限制。
Android 7.0 (API level 24) introduces some additional Android Debug Bridge (ADB) commands that you can use to test app behavior with those background processes disabled:
- To simulate conditions where implicit broadcasts and background services are unavailable, enter the following command:
$ adb shell cmd appops set <package_name> RUN_IN_BACKGROUND ignore
- To re-enable implicit broadcasts and background services, enter the following command:
$ adb shell cmd appops set <package_name> RUN_IN_BACKGROUND allow
来源:https://developer.android.com/topic/performance/background-optimization#further-optimization
您还可以在后台将您的应用设置为 运行:
adb shell am make-uid-idle <package>