Search code examples
androidandroid-serviceandroid-settings

Detect if app background activity is restricted


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:


Solution

  • 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()