diff --git a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt index f9c2eac13..a99cb1148 100644 --- a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt +++ b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt @@ -197,10 +197,18 @@ class SimplexApp: Application(), LifecycleEventObserver { } SimplexService.StartReceiver.toggleReceiver(mode == NotificationsMode.SERVICE) CoroutineScope(Dispatchers.Default).launch { - if (mode == NotificationsMode.SERVICE) + if (mode == NotificationsMode.SERVICE) { SimplexService.start() - else + // Sometimes, when we change modes fast from one to another, system destroys the service after start. + // We can wait a little and restart the service, and it will work in 100% of cases + delay(2000) + if (!SimplexService.isServiceStarted && appPrefs.notificationsMode.get() == NotificationsMode.SERVICE) { + Log.i(TAG, "Service tried to start but destroyed by system, repeating once more") + SimplexService.start() + } + } else { SimplexService.safeStopService() + } } if (mode != NotificationsMode.PERIODIC) { diff --git a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexService.kt b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexService.kt index dd760e0b1..c58959d5c 100644 --- a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexService.kt +++ b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexService.kt @@ -262,7 +262,7 @@ class SimplexService: Service() { private const val SHARED_PREFS_SERVICE_STATE = "SIMPLEX_SERVICE_STATE" private const val WORK_NAME_ONCE = "ServiceStartWorkerOnce" - private var isServiceStarted = false + var isServiceStarted = false private var stopAfterStart = false fun scheduleStart(context: Context) {