Fixed background mode advanced setting switch not starting the service & notification

This commit is contained in:
Sylvain Berfini 2021-08-23 13:52:48 +02:00
parent 2a2315e944
commit 6a76513f1e
2 changed files with 8 additions and 5 deletions

View file

@ -34,13 +34,13 @@ class CoreService : CoreService() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.extras?.get("StartForeground") == true) { if (corePreferences.keepServiceAlive) {
Log.i("[Service] Starting as foreground to keep app alive in background")
coreContext.notificationsManager.startForeground(this, false)
} else if (intent?.extras?.get("StartForeground") == true) {
Log.i("[Service] Starting as foreground due to device boot or app update") Log.i("[Service] Starting as foreground due to device boot or app update")
coreContext.notificationsManager.startForeground(this, true) coreContext.notificationsManager.startForeground(this, true)
coreContext.checkIfForegroundServiceNotificationCanBeRemovedAfterDelay(5000) coreContext.checkIfForegroundServiceNotificationCanBeRemovedAfterDelay(5000)
} else if (corePreferences.keepServiceAlive) {
Log.i("[Service] Starting as foreground to keep app alive in background")
coreContext.notificationsManager.startForeground(this, false)
} }
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
} }

View file

@ -252,7 +252,10 @@ class NotificationsManager(private val context: Context) {
if (coreService != null) { if (coreService != null) {
startForeground(coreService, useAutoStartDescription = false) startForeground(coreService, useAutoStartDescription = false)
} else { } else {
Log.e("[Notifications Manager] Can't start service as foreground, no service!") Log.w("[Notifications Manager] Can't start service as foreground without a service, starting it now")
val intent = Intent()
intent.setClass(coreContext.context, CoreService::class.java)
coreContext.context.startService(intent)
} }
} }