Skip to content

Fix: Resolve ForegroundServiceStartNotAllowedException in MusicService#1198

Merged
theovilardo merged 1 commit intotheovilardo:masterfrom
serein-213:fix/foreground-service-crash
Feb 22, 2026
Merged

Fix: Resolve ForegroundServiceStartNotAllowedException in MusicService#1198
theovilardo merged 1 commit intotheovilardo:masterfrom
serein-213:fix/foreground-service-crash

Conversation

@serein-213
Copy link
Copy Markdown
Contributor

Description:
This PR addresses the android.app.ForegroundServiceStartNotAllowedException crash affecting users on Android 12+ (API 31+), as reported in the recent crash log.

Background

The crash occurs because Android 12+ restricts starting foreground services from the background. In our Media3 implementation, when the MediaNotificationManager asynchronously updates the notification (e.g., after CoilBitmapLoader finishes fetching album art), the app might have already lost its foreground status or playback focus, causing the system to reject the request to start the service in the foreground.
b73c4434c3e873f07460416430b730b3
Problem (Reason for Fix)
The app currently crashes on Android 12+ devices when updating the media notification in certain background states.

Stack Trace:

Click to view full Exception
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.theveloper.pixelplay/.data.service.MusicService
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
    at android.app.ContextImpl.startForegroundService(ContextImpl.java:2020)
    at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:876)
    at androidx.media3.session.MediaNotificationManager.updateNotificationInternal(Unknown Source:36)
    ...

Changes

  • Defensive Notification Updates: Overrode onUpdateNotification in MusicService to perform proactive checks.
  • Auto-Downgrade Strategy: On API 31+, the service will only attempt to start in the foreground if the player is actually set to play (playWhenReady == true) and is not in an IDLE state. If these conditions aren't met, the notification is downgraded to a standard background notification, preventing the crash.
  • Enhanced Error Handling: Added broader try-catch blocks (including Throwable for critical system interrupts) around the notification update pipeline to ensure app stability even if Media3's internal state becomes inconsistent.

Impact

This fix prevents immediate app closure when the media notification is refreshed while the app is in the background, significantly improving reliability on modern Android devices.

Testing

  • Verified that the notification remains visible and interactive during active playback.
  • Confirmed that the service does not crash when the notification is updated while the player is paused in the background.

@serein-213 serein-213 force-pushed the fix/foreground-service-crash branch 2 times, most recently from 5969e9e to b428aeb Compare February 22, 2026 13:44
@serein-213 serein-213 force-pushed the fix/foreground-service-crash branch from b428aeb to a1cc496 Compare February 22, 2026 13:55
@theovilardo theovilardo merged commit 48231be into theovilardo:master Feb 22, 2026
@serein-213 serein-213 deleted the fix/foreground-service-crash branch February 22, 2026 14:36
This was referenced Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants