Skip to content

Fix StrictMode unsafe intent launch violation #2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ This release includes the following changes since the
position artefacts in the Android Auto UI (and other controllers using
this information from the platform media session)
([#1758](https://github.com/androidx/media/issues/1758)).
* Fix StrictMode unsafe launch violation warning
([#2330](https://github.com/androidx/media/pull/2330)).
* Cronet extension:
* Add automatic cookie handling
([#5975](https://github.com/google/ExoPlayer/issues/5975)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ protected final void handleIntentAndMaybeStartTheService(
for (String action : ACTIONS) {
ComponentName mediaButtonServiceComponentName = getServiceComponentByAction(context, action);
if (mediaButtonServiceComponentName != null) {
intent.setComponent(mediaButtonServiceComponentName);
if (!shouldStartForegroundService(context, intent)) {
Intent serviceIntent = new Intent();
serviceIntent.setComponent(mediaButtonServiceComponentName);
serviceIntent.fillIn(intent, 0);
if (!shouldStartForegroundService(context, serviceIntent)) {
Log.i(
TAG,
"onReceive(Intent) does not start the media button event target service into the"
Expand All @@ -184,11 +186,11 @@ protected final void handleIntentAndMaybeStartTheService(
return;
}
try {
ContextCompat.startForegroundService(context, intent);
ContextCompat.startForegroundService(context, serviceIntent);
} catch (/* ForegroundServiceStartNotAllowedException */ IllegalStateException e) {
if (SDK_INT >= 31 && Api31.instanceOfForegroundServiceStartNotAllowedException(e)) {
onForegroundServiceStartNotAllowedException(
intent, Api31.castToForegroundServiceStartNotAllowedException(e));
serviceIntent, Api31.castToForegroundServiceStartNotAllowedException(e));
} else {
throw e;
}
Expand All @@ -214,8 +216,8 @@ protected final void handleIntentAndMaybeStartTheService(
*
* @param context The {@link Context} that {@linkplain #onReceive(Context, Intent) was received by
* the media button event receiver}.
* @param intent The intent that {@linkplain #onReceive(Context, Intent) was received by the media
* button event receiver}.
* @param intent The intent that will be used by {@linkplain
* Context#startForegroundService(Intent) for starting the foreground service}.
* @return true if the service should be {@linkplain ContextCompat#startForegroundService(Context,
* Intent) started as a foreground service}. If false is returned the service is not started
* and the receiver call is a no-op.
Expand Down