[Android] Avoid useless try to start the service in background

Nicolas Pomepuy git at videolan.org
Fri Jan 6 07:20:53 UTC 2023


vlc-android | branch: 3.5.x | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jan  3 13:22:10 2023 +0100| [8015467c1137840dcd5315c2ada5b356b99dc451] | committer: Nicolas Pomepuy

Avoid useless try to start the service in background


(cherry picked from commit 01728559c14b0fc9159be8ee8279512a933b0121)

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

 .../src/main/java/org/videolan/resources/util/Extensions.kt | 13 ++++---------
 1 file changed, 4 insertions(+), 9 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 be3bb2af88..ebbb18e913 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
@@ -87,14 +87,9 @@ suspend fun Context.dbExists(coroutineContextProvider: CoroutineContextProvider
 }
 
 fun Context.launchForeground(intent: Intent) {
-    try {
-        startService(intent)
-    } catch (e: IllegalStateException) {
-        //wait for the UI thread to be ready
-        val ctx = this
-        AppScope.launch(Dispatchers.Main) {
-            intent.putExtra("foreground", true)
-            ContextCompat.startForegroundService(ctx, intent)
-        }
+    val ctx = this
+    AppScope.launch(Dispatchers.Main) {
+        intent.putExtra("foreground", true)
+        ContextCompat.startForegroundService(ctx, intent)
     }
 }
\ No newline at end of file



More information about the Android mailing list