I supposed that if I start a foreground service special use this would automatically make my app to keep running in background but this was wrong.
As shown in Choose the right technology the path led me to "Mannually set a wake lock" as I am not using an API that keeps the device awake.
I tested my app with Pixel 8 Pro A16, Samsung J3 A9. Also Huawei but with App Launch no battery optimization (set manually).
class TimerService : Service() {
...
override fun onCreate() {
super.onCreate()
wakeLock.acquire() <<<<-------
createNotificationChannel()
initTimer()
}
...
private fun stopTimer() {
// Remove callbacks from the background thread handler.
if (::serviceHandler.isInitialized) {
serviceHandler.removeCallbacks(timerRunnable)
}
_isTimerRunning.value = false
_timerStateFlow.value = 0
if (wakeLock.isHeld) { <<<<------
wakeLock.release()
}
}
...
}
Interesting links I found
Hope this helps other devs with the same issue.
Best regards!