79795289

Date: 2025-10-20 21:12:56
Score: 1
Natty:
Report link

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.

enter image description here

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!

Reasons:
  • Blacklisted phrase (0.5): Best regards
  • Blacklisted phrase (1): regards
  • Whitelisted phrase (-1): Hope this helps
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: rsp