I have an android countdown timer that is really inconsistent, and doesn't update the GUI regularly or correctly.
public boolean bTimerRunning = false;
public CountDownTimer timer;
private long currentTime = 180000;
public void onTimerTap(View v) {
if (bTimerRunning) {
//stop timer
timer.cancel();
bTimerRunning = false;
} else {
//start timer
final TextView etTime = (TextView) findViewById(R.id.tvTimer);
//initalize Timer
timer = new CountDownTimer(currentTime, 1000) {
int secondsLeft = 0;
@Override
public void onTick(long millisUntilFinished) {
String currentTimeStr = String.format("%d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
);
//save value to "pause" timer
currentTime = millisUntilFinished;
//show on text
etTime.setText(currentTimeStr);
}
@Override
public void onFinish() {
}
};
timer.start();
//set boolean
bTimerRunning = true;
}
}
From what I have seen in the past SO questions, this is typical for the last few seconds of the timer, but I'm seeing it in the first few ticks, or just in the middle. How can I make my timer a better ticker? Would narrowing down the tick time work and rounding?