Android Best Way of Creating a Timer That Can be Paused/Stopped/Resumed

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
5,315
Hi Guys,

A part of the android app I am building requires me to implement a timer which doesn't have a definite end time (the user will decide when to stop the timer). What would be the best way of doing this? Some threads on StackOverflow point to using Chronometer (will be digesting this in the meantime).

Thanks.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Why do you even need a timer object, when you mention you need something "which doesn't have a definite end time"? Surely a simple boolean (on/off) property would suffice?

Unless of course you wanting to monitor elapsed time between to two events: start / stop. In that case just subtract System.currentTimeMillis() for start and stop.

Anyway Android offers a few timer oriented objects, for example:
  • Choreographer is good to coordinate animation timing.
  • Timer is good to schedule future actions; either once off or repeated at regular intervals.
  • Chronometer; simple count up or down timer.
  • etc...
Which one is best really depends on why you need it.
 
Top