uamp icon indicating copy to clipboard operation
uamp copied to clipboard

Android 9: Background Restriction App Setting

Open two1stnamz opened this issue 5 years ago • 5 comments

Background/setup: I noticed that MainActivity doesn't call MediaBrowseCompat.disconnect() in Activity.onPause(), so I added this to my local code to do proper clean-up.

Issue: I'm noticing that on Android 9+ devices when enabling the "Background Restriction" setting (Settings -> Apps -> UAMP -> Battery -> Background Restriction), MusicService is killed by the OS within a minute of the app going to the background (either via screen lock or going to home) when running on battery.

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 the app is auto-killed by the OS

Shouldn't MusicService be allowed to run in the background due to being a foreground service? Is the "Background Restriction" setting really intended to disallow all background activity?

Interesting find: If I remove the call to MediaBrowserCompat.disconnect() from Activity.onPause(), this problem goes away. Is not calling MediaBrowserCompat.disconnect() really the solution to keeping the MusicService alive (even though it is a foreground service)?

two1stnamz avatar Apr 24 '19 21:04 two1stnamz

That's a good point, though I think I'd move the call to MediaBrowseCompat.disconnect() into the ViewModel.onCleared. If you'd like to make a PR for that, I'll make sure it's merged in.

On the question though, yes, that's actually working as intended.

I had a long conversation with one of the engineers responsible for core framework functionality about this. The key take-away was that if a user enables background restrictions then they have decided the app shouldn't be able to run in the background at all, including for music playback.

They suggested that if an app depended upon background playback (including as a foreground service), the app could:

  • Use ActivityManager.isBackgroundRestricted() to check if background restrictions are in place.
  • If they are, show UI informing the user that music playback may not work the way they'd expect.
  • Optionally, add a button/etc... to learn more and explain the restrictions in more detail.

nic0lette avatar Apr 25 '19 16:04 nic0lette

Thanks for the background info, really helps understand the intent of this setting!

I'll try and get a PR this week.

two1stnamz avatar Apr 29 '19 13:04 two1stnamz

On a side note: In my experience MusicService.onDestroy() is never called if MainActivity doesn't call MediaBrowseCompat.disconnect().

AndrazP avatar May 03 '19 18:05 AndrazP

@AndrazP I observed that as well. From my testing however, when the OS garbage collects the Activity (usually between 30-45mins of inactivity) the service eventually gets killed.

two1stnamz avatar May 03 '19 19:05 two1stnamz

I am using ExoPlayer + mediabrowser and I get reports from users that the audio service (and then the audio notification) gets killed/removed right after the Service is set as a background service. I am not able to reproduce it, but I imagine that it could be caused by many hours of listening, and then when the user pauses the audio, the OS finally gets a chance to remove the Service from memory since it has been running for a very long time. I do call MediaBrowseCompat.disconnect() when the user leaves the app, when my MainActivitys viewmodel onCleared gets invoked. Would you recommend to not call MediaBrowseCompat.disconnect() to keep the background Service alive as long as possible?

finneapps avatar Mar 22 '21 07:03 finneapps