[Android] Services: Stop with stopService()

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


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 22 16:13:40 2020 +0100| [bd0c9cacc7d93abb1b677cfcb0670e76ab407fad] | committer: Geoffrey Métais

Services: Stop with stopService()

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

(cherry picked from commit 6be3523bbb34c4d98da3f2255d44ffa8b66a9a25)

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

 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 81f7ca0b4..6ce022ca9 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -136,7 +136,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
         wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VLC:MediaParsigService")
         wakeLock.acquire()
 
-        if (lastNotificationTime == 5L) stopSelf()
+        if (lastNotificationTime == 5L) stopService(Intent(applicationContext, MediaParsingService::class.java))
         AbstractMedialibrary.getState().observe(this, Observer<Boolean> { running ->
             if (!running && !scanPaused) {
                 exitCommand()
@@ -384,13 +384,14 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
     private fun exitCommand() = launch {
         if (!medialibrary.isWorking && !serviceLock && !discoverTriggered) {
             lastNotificationTime = 0L
-            stopSelf()
+            stopForeground(true)
+            notificationActor.offer(Hide)
+            stopService(Intent(applicationContext, MediaParsingService::class.java))
         }
     }
 
     override fun onDestroy() {
         dispatcher.onServicePreSuperOnDestroy()
-        notificationActor.offer(Hide)
         medialibrary.removeDeviceDiscoveryCb(this)
         unregisterReceiver(receiver)
         localBroadcastManager.unregisterReceiver(receiver)
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index f0b02a927..db53d1bd5 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -536,7 +536,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     }
 
     override fun onTaskRemoved(rootIntent: Intent) {
-        if (settings.getBoolean("audio_task_removed", false)) stopSelf()
+        if (settings.getBoolean("audio_task_removed", false)) stopService(Intent(applicationContext, PlaybackService::class.java))
     }
 
     override fun onDestroy() {
@@ -952,7 +952,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
 
     private fun loadLastAudioPlaylist() {
         if (AndroidDevices.isAndroidTv) return
-        runOnceReady(Runnable { if (!playlistManager.loadLastPlaylist()) stopSelf() })
+        runOnceReady(Runnable { if (!playlistManager.loadLastPlaylist()) stopService(Intent(applicationContext, PlaybackService::class.java)) })
     }
 
     fun loadLastPlaylist(type: Int) {



More information about the Android mailing list