3

Android 9 introduced an option to restrict an app's battery usage (sometimes phrased as limiting app background activity). This option can be set in the app settings:

enter image description here

The effect of this is described briefly on this page: alarms won't be triggered at the specified time presumably, neither will jobs. Not always great for user experience, especially if user is unaware or forgot that this restriction is set.

However I've seen that a few apps manage to detect when this option is set to "Restricted", showing for example a message indicating that some features may misbehave because of it. Which API can be used to detect this? I've tried:

Nicolas
  • 6,611
  • 3
  • 29
  • 73

1 Answers1

5

You can use ActivityManager.isBackgroundRestricted() to check if the background restriction is enabled for your application.

You can read more here: https://developer.android.com/reference/android/app/ActivityManager#isBackgroundRestricted()

Pouya Heydari
  • 2,496
  • 26
  • 39
  • Hi, I know a lot of time has passed since this thread was closed, but I'm facing the same issue. In my settings, there is an "Allow background activity" setting, which is by default switched off. using ActivityManager.isBackgroundRestricted() returns false whether this setting is switched on or off. How can I get this setting's value? – Nitzan Daloomy Apr 14 '22 at 01:47
  • please see [my question](https://stackoverflow.com/q/71865718/8099601) – Nitzan Daloomy Apr 14 '22 at 02:13