Repository navigation
fix: stop the android background service behind its pending start - #211
Closed
tngo-intern wants to merge 1 commit into
Closed
tngo-intern wants to merge 1 commit into
tngo-intern wants to merge 1 commit into
Conversation
stopBackgroundService() called context.stopService() directly. When the app went to the background and straight back, the stop could arrive before the service's onStartCommand() had called startForeground(), and Android 12+ crashed the app with ForegroundServiceDidNotStartInTimeException. Send the service's ACTION_STOP intent with startService() instead. Start commands are delivered in order, so startForeground() always runs first. Skip the stop when this view never started the service, and fall back to stopService() when the app may not start services. Fixes aws#203 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
Closing this; we're keeping the fix in our own project for now. Sorry for the noise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: #203
Description of changes:
startBackgroundService()callsContextCompat.startForegroundService(), andstopBackgroundService()callscontext.stopService(). When the app goes to the background and comes straight back, the stop can arrive before the service'sonStartCommand()has calledstartForeground(). Android 12+ then crashes the app withForegroundServiceDidNotStartInTimeException.This change stops the service by sending its existing
ACTION_STOPintent withstartService(), instead of callingstopService(). Start commands are delivered in order, soonStartCommand()for the pending start, and itsstartForeground()call, always run before the stop. It also skips the stop when this view never started the service. If the app is not allowed to start services at that moment, it falls back tostopService().We carry this as a local patch on 1.6.0 in a production app, with no change to background playback.
#203 suggests calling
startForeground()fromonCreate(). That would also help, and the two changes fit together. Happy to add it here if you prefer.Testing: the change is Kotlin only. I have not run it in this repository's example app.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
🤖 Generated with Claude Code