[Android] Services: Force foreground sooner

Geoffrey Métais git at videolan.org
Tue Mar 31 13:41:12 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar 31 10:04:39 2020 +0200| [32e7f82df15083dfa41f3b8915d7429872e2dc26] | committer: Geoffrey Métais

Services: Force foreground sooner

May help reduce RemoteServiceException occurences

> https://code.videolan.org/videolan/vlc-android/commit/32e7f82df15083dfa41f3b8915d7429872e2dc26
---

 application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt | 1 +
 application/vlc-android/src/org/videolan/vlc/PlaybackService.kt     | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index fc0193325..095ac34f7 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -122,6 +122,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb {
         dispatcher.onServicePreSuperOnCreate()
         super.onCreate()
         NotificationHelper.createNotificationChannels(applicationContext)
+        if (AndroidUtil.isOOrLater) forceForeground()
         medialibrary = Medialibrary.getInstance()
         medialibrary.addDeviceDiscoveryCb(this at MediaParsingService)
         val filter = IntentFilter()
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index dd3de1769..15d2fe781 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -477,6 +477,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
 
         updateHasWidget()
         if (!this::mediaSession.isInitialized) initMediaSession()
+        forceForeground()
 
         val filter = IntentFilter().apply {
             priority = Integer.MAX_VALUE
@@ -523,7 +524,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     }
 
     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
-        if (AndroidUtil.isOOrLater && !isForeground) forceForeground()
+        forceForeground()
         dispatcher.onServicePreSuperOnStart()
         setupScope()
         when (intent?.action) {
@@ -587,6 +588,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
 
     @TargetApi(Build.VERSION_CODES.O)
     private fun forceForeground() {
+        if (!AndroidUtil.isOOrLater || isForeground) return
         val ctx = this at PlaybackService
         val stopped = PlayerController.playbackState == PlaybackStateCompat.STATE_STOPPED
         val notification = if (this::notification.isInitialized && !stopped) notification



More information about the Android mailing list