I am looking to create timed events which trigger UI changes (like a Toast) on Android. I already know you can do this with a Handler
object's postDelayed(runnable, timeDelay)
method (see this page for a great example).
Here is the twist: I also need to be able to pause and resume the countdown for these events. So when a user pauses the on-screen timer it also pauses the countdown before each of these events trigger.
One approach that came to mind was to create a Handler
object on a new Thread
(not the UI thread) and queue up my events using postDelayed
; then I would pause or resume this entire Thread
's execution using this method as needed. The main problem here is figuring out how to make the events run on the UI thread after they have been triggered.
The only other way I can think of would be to check every second that the timer ticks against a list of all my events (brute force) on the UI thread, which is very messy.
If anyone has any thoughts on this or solutions, that would be great!