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

Time:07-20

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.

CodePudding user response:

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.

  • Related