I mm trying to create a resume button in my Android app using Chronometer. So far I have this code snippet for the resume method:
private long pauseResume() {
long timeWhenStopped = chronometer.getBase() - SystemClock.elapsedRealtime();
return timeWhenStopped;
}
And here's the code for the resume button:
public void onClick(View v) {
switch(v.getId()) {
...
case R.id.resume_button:
chronometer.setBase(SystemClock.elapsedRealtime() + pauseResume());
chronometer.start();
break;
}
}
THE PROBLEM: Example, when I pause the timer at 00:05 and then I press the resume button 10 seconds later, the timer will resume counting at 00:15.
I want it to start at 00:05 again, not 00:15, because as it is a "resume" function.
I am very glad you could help. Thanks.
It should by default start from 00:00 when start() is called, and stop() should stop the timer as it is, and the next call to start should resume the time, and have a reset method to reset it to 00:00.
android.widget.Chronometer. Class that implements a simple timer. You can give it a start time in the SystemClock#elapsedRealtime timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start() .
getText(). toString() gives you the total elapsed time in seconds (my application is some kind of stop watch, so you can pause it and resume it). Hope it helps.
A little bit too late but it might help others. I think this is the solution to your problem.
Start / Resume chronometer:
private void chronoStart()
{
// on first start
if ( mLastStopTime == 0 )
mChronometer.setBase( SystemClock.elapsedRealtime() );
// on resume after pause
else
{
long intervalOnPause = (SystemClock.elapsedRealtime() - mLastStopTime);
mChronometer.setBase( mChronometer.getBase() + intervalOnPause );
}
mChronometer.start();
}
Pause chronometer:
private void chronoPause()
{
mChronometer.stop();
mLastStopTime = SystemClock.elapsedRealtime();
}
You need to remember last chronometer stop time.
On resume, you calculate how long it took between the last stop time and the new start time. When you have this interval you just add the interval to your chronometer base time (you "move" original start time).
Hope this helps ;)
Chronometer has used for implements a simple timer.You can give it a start time in the elapsedRealtime()
timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start()
.
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;
here's a kotlin version:
private var chronometerPausedTime: Long = 0
// to start the chronometer with 00:00
private fun Chronometer.startFromZero() {
base = SystemClock.elapsedRealtime()
start()
}
private fun Chronometer.pause() {
chronometerPausedTime = SystemClock.elapsedRealtime() - this.base
stop()
}
private fun Chronometer.resume() {
base = SystemClock.elapsedRealtime() - chronometerPausedTime
chronometerPausedTime = 0
start()
}
to use:
binding.chronometer.startFromZero()
//pause
binding.chronometer.pause()
//resume
binding.chronometer.resume()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With