I have two Chronometer in an activity. First Chronometer is for elapsed time, and Second Chronometer is for interval time.
For Ex.:- We chose 5 Min elapsed time and 30 Seconds interval time, now we start both timers at the same time by pressing btn_start. Both timers start with 0, now when interval timer reaches 00:30 time it restarts again with 0.
Now the issue:- As I have cross checked both timers start with a difference of milliseconds. Which later becomes a difference of 1 second,2 seconds,3 seconds or more seconds.
Below is Custom Chronometer.Java
public class PausableChronometer extends Chronometer implements PlaybackStateView {
// Keeps track of how far long the Chronometer has been tracking when it's paused. We'd like
// to start from this time the next time it's resumed.
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);
Log.e("start()", String.valueOf(SystemClock.elapsedRealtime() + timeWhenStopped));
super.start();
}
@Override
public void stop() {
super.stop();
timeWhenStopped = getBase() - SystemClock.elapsedRealtime();
}
/**
* Reset the timer and start counting from zero.
*/
@Override
public void restart() {
reset();
start();
}
public void reset() {
stop();
setBase(SystemClock.elapsedRealtime());
timeWhenStopped = 0;
}
@Override
public void resume() {
setBase(SystemClock.elapsedRealtime() - timeWhenStopped);
start();
}
@Override
public void pause() {
stop();
timeWhenStopped = SystemClock.elapsedRealtime() - getBase();
}
}
On Button Clicked I execute following:
private void startTimer() {
intervalTimer.start();
elapsedTimer.start();
}
Please let me know, How can I avoid the difference of milliseconds between both Chronometers start time?