【问题标题】:Updating UI with Runnable & Handler Bug使用 Runnable & Handler Bug 更新 UI
【发布时间】:2011-10-25 18:29:47
【问题描述】:

我正在尝试让计时器运行,但是当我的计时器启动时出现了一个奇怪的错误。它从-214342352:00 开始计数。我查看了http://developer.android.com/resources/articles/timed-ui-updates.html,但找不到我的代码有什么问题。

private Handler mHandler = new Handler();
private long mStartTime = 0L;

开始计时

if (mStartTime == 0L) {
             mStartTime = System.currentTimeMillis();
             mHandler.removeCallbacks(mUpdateTimeTask);
             mHandler.postDelayed(mUpdateTimeTask, 1000);
}

可运行

public Runnable mUpdateTimeTask = new Runnable() {
       public void run() {


           final long start =  mStartTime;
           long millis = SystemClock.uptimeMillis()-start;
           int seconds = (int) (millis / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

           if (seconds < 10) {
              alarmCounter.setText("" + minutes + ":0" + seconds);
           } else {
              alarmCounter.setText("" + minutes + ":" + seconds);            
           }

         //  mHandler.postAtTime(this,start + (((minutes * 60) + seconds + 1) * 1000));
        mHandler.postDelayed(this, 1000);

       }
    };

【问题讨论】:

    标签: android timer handler runnable stopwatch


    【解决方案1】:

    您正在使用两种不同的测量系统。使用SystemClock.uptimeMillis()System.currentTimeMillis() 进行所有测量。

    【讨论】:

    • 是的。 uptimemillis 会比 currenttimemillis 小很多
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多