[Android] Prevent ForegroundServiceStartNotAllowedException

Nicolas Pomepuy git at videolan.org
Wed Feb 8 13:19:16 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Feb  3 09:08:36 2023 +0100| [095506fd15cf5bd43a3abe8a7d5613c8b6dca8f2] | committer: Nicolas Pomepuy

Prevent ForegroundServiceStartNotAllowedException

Fixes #2784

> https://code.videolan.org/videolan/vlc-android/commit/095506fd15cf5bd43a3abe8a7d5613c8b6dca8f2
---

 .../main/java/org/videolan/resources/util/Extensions.kt | 11 ++++++++++-
 .../src/org/videolan/vlc/MediaParsingService.kt         | 17 +++++++++++++++--
 2 files changed, 25 insertions(+), 3 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 b1e8410aab..249dca2d2c 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
@@ -1,8 +1,11 @@
 package org.videolan.resources.util
 
+import android.app.ForegroundServiceStartNotAllowedException
 import android.app.Service
 import android.content.Context
 import android.content.Intent
+import android.os.Build
+import android.util.Log
 import android.content.pm.PackageInfo
 import android.content.pm.PackageManager
 import android.os.Build.VERSION.SDK_INT
@@ -94,7 +97,13 @@ fun Context.launchForeground(intent: Intent, block: () -> Unit = {}) {
     val ctx = this at launchForeground
     AppScope.launch(Dispatchers.Main) {
         intent.putExtra("foreground", true)
-        ContextCompat.startForegroundService(ctx, intent)
+        try {
+            ContextCompat.startForegroundService(ctx, intent)
+        } catch (e: Exception) {
+            if (SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
+                Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
+            }
+        }
         block()
     }
 }
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index d2a0db0870..81c9fbef73 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -24,6 +24,7 @@ package org.videolan.vlc
 
 import android.annotation.SuppressLint
 import android.annotation.TargetApi
+import android.app.ForegroundServiceStartNotAllowedException
 import android.content.BroadcastReceiver
 import android.content.Context
 import android.content.Intent
@@ -201,7 +202,13 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
     @TargetApi(Build.VERSION_CODES.O)
     private fun forceForeground() {
         val notification = NotificationHelper.createScanNotification(applicationContext, getString(R.string.loading_medialibrary), scanPaused, -1, -1)
-        startForeground(43, notification)
+        try {
+            startForeground(43, notification)
+        } catch (e: Exception) {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
+                Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
+            }
+        }
     }
 
     private fun discoverStorage(path: String) {
@@ -377,7 +384,13 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
             if (lastNotificationTime != -1L) {
                 try {
                     val notification = NotificationHelper.createScanNotification(applicationContext, progressText, scanPaused, scheduled, done)
-                    startForeground(43, notification)
+                    try {
+                        startForeground(43, notification)
+                    } catch (e: Exception) {
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
+                            Log.w("MediaParsingService", "ForegroundServiceStartNotAllowedException caught!")
+                        }
+                    }
                 } catch (ignored: IllegalArgumentException) {}
                 progressText
             } else ""



More information about the Android mailing list