倒数计时器不更新Android中的分钟和小时

时间:2015-04-03 15:29:49

标签: android countdowntimer

我正在使用countDownTimer,它不会更新倒计时。我正在倒数几分钟。只有当我重新启动活动时才会更新倒计时。它没有更新tick。 请帮我。谢谢 这是我的代码:

Date eventStartDate = Utility.stringToDate(event.eventFrom);
            Date currentDateTime = new Date();
            long ms = eventStartDate.getTime() - currentDateTime.getTime();

            long ms1 = eventStartDate.getTime() - currentDateTime.getTime();
            if (ms > 0) {
                long miliSecondsPerSecond = 1000;
                long miliSecondsPerMinute = miliSecondsPerSecond * 60;
                long miliSecondsPerHour = miliSecondsPerMinute * 60;
                long miliSecondsPerDay = miliSecondsPerHour * 24;
                long days = ms / miliSecondsPerDay;
                ms = ms - (days * miliSecondsPerDay);
                final long hours = ms / miliSecondsPerHour;
                ms = ms - (hours * miliSecondsPerHour);
               final  long minutes = ms / miliSecondsPerMinute;
                ms = ms - (minutes * miliSecondsPerMinute);


                new CountDownTimer(ms1, miliSecondsPerMinute) { // adjust the milli seconds here

                    public void onTick(long millisUntilFinished) {
                        eventTimer.setText(hours + ":" + minutes);
                    }

                    public void onFinish() {
                        eventTimer.setText("00:00");
                    }
                }.start();

1 个答案:

答案 0 :(得分:1)

在OnTick中编写以下代码

 Date eventStartDate = Utility.stringToDate(event.eventFrom);
 Date currentDateTime = new Date();
 long ms = eventStartDate.getTime() - currentDateTime.getTime();
 long ms1 = eventStartDate.getTime() - currentDateTime.getTime();
  if (ms > 0)
    {
           long miliSecondsPerSecond = 1000;
           long miliSecondsPerMinute = miliSecondsPerSecond * 60;
           long miliSecondsPerHour = miliSecondsPerMinute * 60;
           long miliSecondsPerDay = miliSecondsPerHour * 24;
           long days = ms / miliSecondsPerDay;
           ms = ms - (days * miliSecondsPerDay);
           final long hours = ms / miliSecondsPerHour;
           ms = ms - (hours * miliSecondsPerHour);
           final  long minutes = ms / miliSecondsPerMinute;
           ms = ms - (minutes * miliSecondsPerMinute);
}