android: trying to start service again in case it was destroyed (#3745)

This commit is contained in:
Stanislav Dmitrenko 2024-01-24 23:22:29 +07:00 committed by GitHub
parent bd30b80e15
commit f81e457e09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -197,10 +197,18 @@ class SimplexApp: Application(), LifecycleEventObserver {
} }
SimplexService.StartReceiver.toggleReceiver(mode == NotificationsMode.SERVICE) SimplexService.StartReceiver.toggleReceiver(mode == NotificationsMode.SERVICE)
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
if (mode == NotificationsMode.SERVICE) if (mode == NotificationsMode.SERVICE) {
SimplexService.start() 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() SimplexService.safeStopService()
}
} }
if (mode != NotificationsMode.PERIODIC) { if (mode != NotificationsMode.PERIODIC) {

View File

@ -262,7 +262,7 @@ class SimplexService: Service() {
private const val SHARED_PREFS_SERVICE_STATE = "SIMPLEX_SERVICE_STATE" private const val SHARED_PREFS_SERVICE_STATE = "SIMPLEX_SERVICE_STATE"
private const val WORK_NAME_ONCE = "ServiceStartWorkerOnce" private const val WORK_NAME_ONCE = "ServiceStartWorkerOnce"
private var isServiceStarted = false var isServiceStarted = false
private var stopAfterStart = false private var stopAfterStart = false
fun scheduleStart(context: Context) { fun scheduleStart(context: Context) {