Android_Chronometer pause

andveand picture andveand · Apr 8, 2011 · Viewed 17.8k times · Source

I want to pause chronometer and after I click the button I want to continue chromoneter to count... I search but couldn't a function related this.. how can do it?

Answer

HenrikS picture HenrikS · Aug 15, 2011

You are gonna need a variable that keeps track on the time that has passed since the Chronometer was started:

long timeWhenStopped = 0;

Update the value of the variable when you stop the chronometer like this:

timeWhenStopped = mChronometer.getBase() - SystemClock.elapsedRealtime();
mChronometer.stop();

We will also use this variable to adjust the chronometer before starting it:

mChronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
mChronometer.start();

And finally if you have a way to reset your chronometer then you should remember to also reset the timeWhenStopped variable. Something like this:

mChronometer.setBase(SystemClock.elapsedRealtime());
timeWhenStopped = 0;