How can I keep a countdown timer running even if I don't open the app for a long time?

Issue

I’ve been making a timer app using CountDownTimer, and now I’m testing if it works properly. However, if I set a bit longer period, for example 1h30m, and close the app, it stops working, and when I open the app, it shows the initial screen, the timer I set disappears.
I tried to turn off "Battery Optimization" in this app.
I tried to turn on "Background Work" in this app.
But they didn’t work.

Solution

You don’t. You persist the time the countdown counts toward when the user starts it. If the application closes,then the user opens the application again, you set up the CountDownTimer component with the proper remaining time. If you want to do something when the countdown ends, even when the user is not using the application, you can use AlarmManager to wake up the device.

Answered By – Tamas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published