6

I'm working on a music player app and I'm noticing weird behavior on Android 9 devices when a user enables the "Background Restriction" setting (Settings -> Apps -> [App Name] -> Battery -> Background Restriction).

Here's what I'm doing: I start my music player service by calling Service.startService() then set it to foreground via Service.startForeground() while my app is in the foreground.

Here's what I'm seeing when "Background Restriction" is turned on: 1) Service.startForeground() will not posting a notification 2) My foreground service is killed by the OS within a minute of my app going to the background

Here's what I see in the logs: 1) "Service.startForeground() not allowed due to bg restriction" when calling Service.startForeground() 2) "Stopping service due to app idle" when my app is auto-killed by the OS

Here's my question: I thought the whole point of a foreground service is to allow background processing with the user's knowledge (an ongoing notification); is the "Background Restriction" setting really intended to disallow all background activity?

Interesting find: Looking at Google's "Universal Music Player" sample project on GitHub, I noticed that their sample project is not being killed like my app is. After digging I noticed this is because they are binding to their service and never unbinding in Activity.onPause(). According to Google's docs, bound services are not subject to the same background restrictions. Is this really the solution to my problem? Seems a little hacky/fragile.

Thanks in advance for the help!

two1stnamz
  • 608
  • 1
  • 6
  • 18

1 Answers1

9

Here's what I've found:

  • "Background Restriction" (or "Allow Background Activity" on some devices) is intended to stop ALL background activity regardless of whether your service has called setForeground()

  • There is no way around this setting. You cannot programmatically disable it. Your only option is to programmatically check if it's enabled using ActivityManager.isBackgroundRestricted() and display a pop-up informing your users on how to disable this setting

  • Google's Universal Music Player sample project on GitHub happens to work (as of the writing of this answer) only because a service bind is not released when the main Activity is paused. The sample project's service is however killed when the main Activity is garbage collected (typically 30-45 minutes depending on the device).

two1stnamz
  • 608
  • 1
  • 6
  • 18
  • 1
    Do you know what Intent I can use to open "Background Restriction" from my app and show it to user? – AVEbrahimi Nov 07 '19 at 07:22
  • 1
    Good question. I haven't found an intent that does that. What I've done in my apps is provide a tutorial of how to get to the setting, and launch the app's settings Activity via android.provider.Settings.ACTION_APPLICATION_SETTINGS to get the user started. – two1stnamz Nov 07 '19 at 13:36
  • Any source for this (android doc links)? – Samuel Eminet Oct 28 '20 at 17:09
  • Unfortunately no, not that I've found; this info was provided though a conversation with a Google engineer. I'm unsure how this has changed in Android 10 and 11 as I haven't looked. Please post links here if you happen to find anything! – two1stnamz Nov 05 '20 at 11:51
  • 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:50
  • please see [my question](https://stackoverflow.com/q/71865718/8099601) – Nitzan Daloomy Apr 14 '22 at 02:13