Android,CountDownTimer延迟

时间:2016-10-24 10:05:43

标签: android

我正在使用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是〜29953millisUntilFinished = millisUntilFinished / 1000等于29,而我期待30! 那么如何在不使用counter值的情况下处理这种延迟呢?我使用了BigdecimalRounding,但仍然传递的值小于30000

1 个答案:

答案 0 :(得分:1)

试试这个:millisUntilFinished = Math.round(millisUntilFinished / 1000.0f);