[Android] Prevent ForegroundServiceStartNotAllowedException
Nicolas Pomepuy
git at videolan.org
Wed Feb 8 14:23:25 UTC 2023
vlc-android | branch: 3.5.x | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Feb 3 09:08:36 2023 +0100| [e708da8a58e882d75104e261012148ecbae4eca2] | committer: Duncan McNamara
Prevent ForegroundServiceStartNotAllowedException
Fixes #2784
> https://code.videolan.org/videolan/vlc-android/commit/e708da8a58e882d75104e261012148ecbae4eca2
---
.../main/java/org/videolan/resources/util/Extensions.kt | 17 ++++++++++++++++-
.../src/org/videolan/vlc/MediaParsingService.kt | 17 +++++++++++++++--
2 files changed, 31 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 d47164b08e..0d03b51b43 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,7 +1,16 @@
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
+import android.os.Bundle
+import android.os.Parcelable
import android.os.Handler
import android.os.Looper
import androidx.core.content.ContextCompat
@@ -90,7 +99,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()
}
}
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 11214427e4..076b3b3ddc 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
@@ -200,7 +201,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) {
@@ -376,7 +383,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