From 65c579813b69cdc9c0089f2cd6064e151bc9291f Mon Sep 17 00:00:00 2001 From: theov Date: Thu, 7 May 2026 23:53:52 -0300 Subject: [PATCH] Handle BackgroundServiceStartNotAllowedException when starting MusicService Add a catch block for `BackgroundServiceStartNotAllowedException` in `MusicService` to prevent crashes when `startForegroundService` is called while the app is in a background-cached state. This ensures the service fails gracefully when background execution limits are enforced. --- .../pixelplay/data/service/MusicService.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/theveloper/pixelplay/data/service/MusicService.kt b/app/src/main/java/com/theveloper/pixelplay/data/service/MusicService.kt index 5c6d0ea9e..23912a10c 100644 --- a/app/src/main/java/com/theveloper/pixelplay/data/service/MusicService.kt +++ b/app/src/main/java/com/theveloper/pixelplay/data/service/MusicService.kt @@ -1,6 +1,7 @@ -package com.theveloper.pixelplay.data.service +package com.theveloper.pixelplay.data.service import android.app.AlarmManager +import android.app.BackgroundServiceStartNotAllowedException import android.app.ForegroundServiceStartNotAllowedException import android.app.PendingIntent import android.content.ComponentName @@ -2693,6 +2694,15 @@ class MusicService : MediaLibraryService() { "startForegroundService not allowed; ignoring redundant self-start request" ) serviceIntent?.component ?: ComponentName(this, javaClass) + } catch (e: BackgroundServiceStartNotAllowedException) { + // Thrown when startForegroundService() itself is called while the app is in a + // background-cached state (distinct from ForegroundServiceStartNotAllowedException). + // Safe to swallow: the service is either already running or Media3 will retry. + Timber.tag(TAG).w( + e, + "startForegroundService blocked (app in background); ignoring self-start request" + ) + serviceIntent?.component ?: ComponentName(this, javaClass) } } return super.startForegroundService(serviceIntent)