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 ebbb18e91..d47164b08 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 @@ -86,10 +86,11 @@ suspend fun Context.dbExists(coroutineContextProvider: CoroutineContextProvider File(getDir("db", Context.MODE_PRIVATE).toString() + Medialibrary.VLC_MEDIA_DB_NAME).exists() } -fun Context.launchForeground(intent: Intent) { - val ctx = this +fun Context.launchForeground(intent: Intent, block: () -> Unit = {}) { + val ctx = this@launchForeground AppScope.launch(Dispatchers.Main) { intent.putExtra("foreground", true) ContextCompat.startForegroundService(ctx, intent) + block() } } \ No newline at end of file diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt index 807408e51..0d6d96b0b 100644 --- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt +++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt @@ -901,13 +901,15 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc if (isPlayingPopup) return@launch if (!AndroidUtil.isLolliPopOrLater || playing || audioFocusHelper.lossTransient) { if (!isForeground) { - this@PlaybackService.startForeground(3, notification) - isForeground = true + ctx.launchForeground(Intent(ctx, PlaybackService::class.java)) { + ctx.startForeground(3, notification) + isForeground = true + } } else NotificationManagerCompat.from(ctx).notify(3, notification) } else { if (isForeground) { - ServiceCompat.stopForeground(this@PlaybackService, ServiceCompat.STOP_FOREGROUND_DETACH) + ServiceCompat.stopForeground(ctx, ServiceCompat.STOP_FOREGROUND_DETACH) isForeground = false } NotificationManagerCompat.from(ctx).notify(3, notification)