[Android] Services: Stop with stopService()

Geoffrey Métais git at videolan.org
Thu Jan 23 14:05:04 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 22 16:13:40 2020 +0100| [6be3523bbb34c4d98da3f2255d44ffa8b66a9a25] | committer: Nicolas Pomepuy

Services: Stop with stopService()

Calling stopSelf() could cause the service to be destroyed before
calling setForeground() and lead to a RemoteException

> https://code.videolan.org/videolan/vlc-android/commit/6be3523bbb34c4d98da3f2255d44ffa8b66a9a25
---

 vlc-android/src/org/videolan/vlc/MediaParsingService.kt | 7 ++++---
 vlc-android/src/org/videolan/vlc/PlaybackService.kt     | 4 ++--
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 03a15e079..b880bed20 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -141,7 +141,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb, LifecycleOwn
         wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VLC:MediaParsigService")
         wakeLock.acquire()
 
-        if (lastNotificationTime == 5L) stopSelf()
+        if (lastNotificationTime == 5L) stopService(Intent(applicationContext, MediaParsingService.javaClass))
         Medialibrary.getState().observe(this, Observer<Boolean> { running ->
             if (!running && !scanPaused) {
                 exitCommand()
@@ -392,13 +392,14 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb, LifecycleOwn
             } catch (e: Exception) {
                 if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "${e.cause}")
             }
-            stopSelf()
+            stopForeground(true)
+            notificationActor.offer(Hide)
+            stopService(Intent(applicationContext, MediaParsingService.javaClass))
         }
     }
 
     override fun onDestroy() {
         dispatcher.onServicePreSuperOnDestroy()
-        notificationActor.offer(Hide)
         medialibrary.removeDeviceDiscoveryCb(this)
         unregisterReceiver(receiver)
         if (wakeLock.isHeld) wakeLock.release()
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 015565523..c2510a118 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -562,7 +562,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     }
 
     override fun onTaskRemoved(rootIntent: Intent) {
-        if (settings.getBoolean("audio_task_removed", false)) stopSelf()
+        if (settings.getBoolean("audio_task_removed", false)) stopService(Intent(applicationContext, PlaybackService.javaClass))
     }
 
     override fun onDestroy() {
@@ -997,7 +997,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
         if (AndroidDevices.isAndroidTv) return
         scope.launch {
             awaitMedialibraryStarted()
-            if (!playlistManager.loadLastPlaylist()) stopSelf()
+            if (!playlistManager.loadLastPlaylist()) stopService(Intent(applicationContext, PlaybackService.javaClass))
         }
     }
 



More information about the Android mailing list