[Android] Fix ForegroundServiceStartNotAllowedException

Nicolas Pomepuy git at videolan.org
Wed Sep 9 07:31:19 UTC 2026


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Sep  4 10:28:03 2026 +0200| [5af884577a7dccf525655996ea1f3ea2753f645f] | committer: Duncan McNamara

Fix ForegroundServiceStartNotAllowedException

> https://code.videolan.org/videolan/vlc-android/commit/5af884577a7dccf525655996ea1f3ea2753f645f
---

 .../java/org/videolan/resources/util/Extensions.kt    | 19 ++++++++++++++-----
 .../src/org/videolan/vlc/PlaybackService.kt           | 10 ++++++----
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
index 48f63ec35b..ce5af19613 100644
--- a/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+++ b/application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
@@ -204,12 +204,21 @@ fun Service.stopForegroundCompat(removeNotification:Boolean = true) = when {
  * @param serviceNotificationId the notification id to be used. Depends on the service type
  * @param notification the notification to display
  * @param foregroundServiceType the foreground service type, needed for API >= 33
+ * @return true if startForeground succeeded, false if ForegroundServiceStartNotAllowedException occurred
  */
-fun Service.startForegroundCompat(serviceNotificationId:NotificationIds, notification:Notification, foregroundServiceType: Int) {
-    if (SDK_INT >= Build.VERSION_CODES.Q)
-        startForeground(serviceNotificationId.id, notification, foregroundServiceType)
-    else
-        startForeground(serviceNotificationId.id, notification)
+fun Service.startForegroundCompat(serviceNotificationId: NotificationIds, notification: Notification, foregroundServiceType: Int): Boolean {
+    return try {
+        if (SDK_INT >= Build.VERSION_CODES.Q)
+            startForeground(serviceNotificationId.id, notification, foregroundServiceType)
+        else
+            startForeground(serviceNotificationId.id, notification)
+        true
+    } catch (e: Exception) {
+        if (SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
+            Log.w("Service", "ForegroundServiceStartNotAllowedException caught in startForegroundCompat", e)
+            false
+        } else throw e
+    }
 }
 
 /**
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index d7b531eeb0..ace1540912 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -951,8 +951,9 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
                     ctx.resources.getString(R.string.loading), "", "", null, false, true,
                     true, speed, isPodcastMode, false, enabledActions, null, pi)
         }
-        startForegroundCompat(NotificationIds.PLAYBACK, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
-        isForeground = true
+        if (startForegroundCompat(NotificationIds.PLAYBACK, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)) {
+            isForeground = true
+        }
         if (stopped) lifecycleScope.launch { hideNotification(true) }
     }
 
@@ -1061,8 +1062,9 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
                     if (!VlcMigrationHelper.isLolliPopOrLater || playing || audioFocusHelper.lossTransient) {
                         if (!isForeground) {
                             ctx.launchForeground(Intent(ctx, PlaybackService::class.java)) {
-                                startForegroundCompat(NotificationIds.PLAYBACK, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
-                                isForeground = true
+                                if (startForegroundCompat(NotificationIds.PLAYBACK, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)) {
+                                    isForeground = true
+                                }
                             }
                         } else
                             NotificationManagerCompat.from(ctx).notify(NotificationIds.PLAYBACK.id, notification)



More information about the Android mailing list