Android_Chronometer pause Android_Chronometer pause android android

Android_Chronometer pause


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;


The two other answers are identical, and work very well on the Chronometer's display, but they have one flaw: timeWhenStopped, as well as the value returned by getCurrentTime(), is negative.

Here is my suggestion, base on these two answers:

public class PausableChronometer extends Chronometer {    private long timeWhenStopped = 0;    public PausableChronometer(Context context) {        super(context);    }    public PausableChronometer(Context context, AttributeSet attrs) {        super(context, attrs);    }    public PausableChronometer(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    public void start() {        setBase(SystemClock.elapsedRealtime() - timeWhenStopped);        super.start();    }    @Override    public void stop() {        super.stop();        timeWhenStopped = SystemClock.elapsedRealtime() - getBase();    }    public void reset() {        stop();        setBase(SystemClock.elapsedRealtime());        timeWhenStopped = 0;    }    public long getCurrentTime() {        return timeWhenStopped;    }    public void setCurrentTime(long time) {        timeWhenStopped = time;        setBase(SystemClock.elapsedRealtime() - timeWhenStopped);    }}

You've got to understand SystemClock.elapsedRealtime() as an indicator for "now". So when we (re)start the chronometer, we shall set the base N seconds in the past, N being the timer's current value (0 in case of first start).Similarly, when stopping the chronometer, the time displayed by the chronometer is the time elapsed between the previously set base (getBase()) and now (SystemClock.elapsedRealtime()), hence the subtraction.


I made a PauseableChronometer class for this.

import android.content.Context;import android.os.SystemClock;import android.util.AttributeSet;import android.widget.Chronometer;public class PausableChronometer extends Chronometer {    private long timeWhenStopped = 0;    public PausableChronometer(Context context) {        super(context);    }    public PausableChronometer(Context context, AttributeSet attrs) {        super(context, attrs);    }    public PausableChronometer(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    public void start() {        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);        super.start();    }    @Override    public void stop() {        super.stop();        timeWhenStopped = getBase() - SystemClock.elapsedRealtime();    }    public void reset() {        stop();        setBase(SystemClock.elapsedRealtime());        timeWhenStopped = 0;    }    public long getCurrentTime() {        return timeWhenStopped;    }    public void setCurrentTime(long time) {        timeWhenStopped = time;        setBase(SystemClock.elapsedRealtime()+timeWhenStopped);    }}