我正在使用CountDownTimer
:
timer = new CountDownTimer(30 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
handleOnTick(millisUntilFinished);
}
@Override
public void onFinish() {
handleOnFinish();
playSound(ALERT_SECOND_FINISH);
}
};
timer.start();
}
private void handleOnTick(long millisUntilFinished) {
millisUntilFinished = millisUntilFinished / 1000;
tvTime.setText(
String.format(Locale.US,
"%d:%02d",
min, sec)
);
}
问题是,当handleOnTick
被调用时,millisUntilFinished
是〜29953
而millisUntilFinished = millisUntilFinished / 1000
等于29,而我期待30!
那么如何在不使用counter
值的情况下处理这种延迟呢?我使用了Bigdecimal
和Rounding
,但仍然传递的值小于30000
。
答案 0 :(得分:1)
试试这个:millisUntilFinished = Math.round(millisUntilFinished / 1000.0f);