Wednesday, July 18, 2012

Timer in Android ?



Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:
CountDownTimer waitTimer=new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();


to stop the timer early we can call:

     if(waitTimer != null) {
         waitTimer.cancel();
         waitTimer = null;
     }

No comments:

Post a Comment