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?

Arnav M.
  • 2,590
  • 1
  • 27
  • 44
  • There are no restrictions in android versions below API 26 – PEHLAJ Sep 20 '17 at 07:37
  • @user12345 i'm asking for app running at API 26, targeting below API 26. – Arnav M. Sep 20 '17 at 07:40
  • Which android version is installed in emulator? – PEHLAJ Sep 20 '17 at 07:43
  • API 26= Android O – Arnav M. Sep 20 '17 at 07:43
  • You might need to run on real device then. – PEHLAJ Sep 20 '17 at 07:44
  • probably they mean in Developer Options -> Background Processes -> No Background Processes instead of Standard Limit – matrix Sep 20 '17 at 07:46
  • @matrix that setting is different thing. – Arnav M. Sep 20 '17 at 08:35
  • @user12345 post a screenshot here, it's not a thing to be excluded from emulator. – Arnav M. Sep 20 '17 at 08:35
  • 1
    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](https://commonsware.com/blog/2017/05/24/android-o-background-limitations-not-just-targetsdkversion-o.html) for some background on this. However, I have not yet been able to reproduce this in production. – CommonsWare Sep 20 '17 at 11:20
  • @CommonsWare thanks for clarifying. So developers should not consider that option. If you want apps running on O, abide by the background limits!! – Arnav M. Sep 20 '17 at 12:35
  • 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. – CommonsWare Sep 20 '17 at 12:43
  • @CommonsWare In that case, target any API, all have same effect. – Arnav M. Sep 20 '17 at 12:47

2 Answers2

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.

Arnav M.
  • 2,590
  • 1
  • 27
  • 44
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

Community
  • 1
  • 1
xmarcusv
  • 63
  • 6