Android O Settings to enable background restriction for applications
Asked Answered
D

2

9

As per the 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.

I'm targeting Below API 26 but i cannot find the settings to enable the restriction in official android emulator.

Where is this setting available?

Duster answered 20/9, 2017 at 7:31 Comment(12)
There are no restrictions in android versions below API 26Dianoetic
@user12345 i'm asking for app running at API 26, targeting below API 26.Duster
Which android version is installed in emulator?Dianoetic
API 26= Android ODuster
You might need to run on real device then.Dianoetic
probably they mean in Developer Options -> Background Processes -> No Background Processes instead of Standard LimitLongshore
@Longshore that setting is different thing.Duster
@user12345 post a screenshot here, it's not a thing to be excluded from emulator.Duster
It is supposed to show up on a Settings page for the app, for apps that tend to consume a lot of battery. See my May blog post for some background on this. However, I have not yet been able to reproduce this in production.Iodous
@Iodous thanks for clarifying. So developers should not consider that option. If you want apps running on O, abide by the background limits!!Duster
It is safest to assume that your app might be subject to 8.0's background execution limits, even if your targetSdkVersion is below 26. In some cases, you still do not need to make any changes. For example, if you have a service that downloads a file, and you are sure that the service will be done in less than a minute, the background restrictions will not affect you.Iodous
@Iodous In that case, target any API, all have same effect.Duster
D
6

The setting to enable the background restriction is available in App Info -> Battery screen.

Not reproducible in emulator yet.

The option is not available when you initially install the application (targeting below API 26) until the app does some background work.

So i was able to produce it by-

  1. Targeting Application at API 25
  2. Create A long running service (around 5 minutes)
  3. Starting the service from onCreate of MainActivity
  4. Send App to background(press back) and leave device on Battery(No power connected)

Within a few minutes, the option will be available to enable the restrictions.

Duster answered 13/10, 2017 at 6:8 Comment(0)
H
3

You can use adb to enable/disable the background limitations.

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

Source: https://developer.android.com/topic/performance/background-optimization#further-optimization

And you can also set your app to run in background:

adb shell am make-uid-idle <package>

Source: Android Oreo Background Execution Limits

Hanschen answered 8/10, 2018 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.