HH:MM:SS Countdowntimer

时间:2012-03-05 22:05:59

标签: android countdowntimer

我无法弄清楚如何使用毫秒制作HH:MM:SS Countdowntimer。 我需要制作一个CountdownTimer,告诉我们到第二天要剩下多少小时,分钟和秒。这是我的代码:

  final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   long timer = prefs.getLong("TIME", 86400000);
   new CountDownTimer(timer, 1000) {

         public void onTick(long elapsed) {

                                        time.setText(elapsed/3600000+":"+elapsed/1440000+":"+elapsed/?);
                              SharedPreferences.Editor editor = prefs.edit();
                              editor.putLong("TIME",elapsed);
                              editor.commit(); 
         }
         public void onFinish() {

         }
      }.start();

P.S:此外,它必须节省到最后一次检查所剩的时间。我很期待你们的回复。提前谢谢!

1 个答案:

答案 0 :(得分:5)

你的计算错了,应该是:

final static long seconds_in_milllies = 1000L;
final static long minutes_in_millies = seconds_in_millies * 60;
final static long hours_in_millies = minutes_in_millies * 60;

// in onTick
long hours = elapsed / hours_in_millies;
elapsed %= hours_in_millies;
long minutes = elapsed / minutes_in_millies;
elapsed %= minutes_in_millies;
long seconds = elapsed / seconds_in_millies;
time.setText(hours + ":" + minutes + ":" + seconds );