newbie
newbie

Reputation: 958

Updating UI with Runnable & Handler Bug

I am trying to get a timer running, but I get a weird bug when my timer starts. It starts counting from -214342352:00. I have looked through http://developer.android.com/resources/articles/timed-ui-updates.html but couldn't find what's wrong with my code.

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

Start timer

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

Runnable

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);

       }
    };

Upvotes: 0

Views: 701

Answers (1)

Justin Breitfeller
Justin Breitfeller

Reputation: 13801

You are using two different systems of measurement. Either use SystemClock.uptimeMillis() or use System.currentTimeMillis() for all your measurements.

Upvotes: 3

Related Questions