A
A
artem2021-01-29 14:28:22
Android
artem, 2021-01-29 14:28:22

How to create a timer at the end of which lives are restored?

Hello, I have an application, like a quiz, I need to make a system of lives, or rather, their restoration. When there are less than three lives, a timer should start for an hour and count down by a second, this should be displayed in the window with lives.
After I exit the application, I need the timer to continue running.
I've been googling for the second day, I found only timertask, but I don't quite understand how to work with it. I will be grateful for your help! Perhaps the solution is quite simple, but I do not understand, just started reading about android. (1.5 months)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-01-29
@zagayevskiy

At the moment when the countdown should start, store the current time in persistent storage, and also start the countdown in an arbitrary way (for example, postDelayed, or Observable::interval). At the next countdown step, count how much time has passed between the moment you saved and the current moment. Stop the countdown when the application stops (for example in Activity::onStop, Fragment::onDestroyView, etc).
When restoring the state, simply count how much time has passed, and either start the countdown again, or do what should be after the countdown.
Something like this. Of course, all this should be beautifully encapsulated, it's just the main idea.

K
Korben5E, 2021-02-03
@Korben5E

Similar things are done on the server.
In theory, you should have a database where statistics are actually collected (of all who use the application) and the fact of wasting life is recorded.
The application just reads the information at startup and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question