[Android] Wait for the UI thread to be ready before starting foreground services

Nicolas Pomepuy git at videolan.org
Tue Nov 30 06:26:37 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Nov 29 10:12:11 2021 +0100| [ef27cc72e5e600afe6ba86b0a45e8045c71f4027] | committer: Nicolas Pomepuy

Wait for the UI thread to be ready before starting foreground services

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

 .../src/main/java/org/videolan/resources/util/Extensions.kt       | 8 ++++++--
 1 file changed, 6 insertions(+), 2 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 044e5fe81..a17083948 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
@@ -93,7 +93,11 @@ fun Context.launchForeground(intent: Intent) {
     try {
         startService(intent)
     } catch (e: IllegalStateException) {
-        intent.putExtra("foreground", true)
-        ContextCompat.startForegroundService(this, intent)
+        //wait for the UI thread to be ready
+        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