Home>

countdowntimer

countdowntimer is a countdown class that comes with android,Use this class to easily implement the countdown function

How countdowntimer is implemented

new countdowntimer (6000,1000) {//The first parameter represents the total time of the countdown,The second parameter indicates the countdown interval.
          @override
          public void ontick (long millisuntilfinished) {//The countdown process
            textview.settext (millisuntilfinished/1000 + "seconds");
          }
          @override
          public void onfinish () {//The countdown ends
            textview.settext ("End of countdown");
          }
        } .start ();

Achieve effect

Cancel timer

Calling the cancel () method of countdowntimer will cancel the timer for us:but this method,Only works on android 5.0 and above.It has no effect under android 5.0.If you need to use cancel in systems below android 5.0, you need to implement it yourself based on the countdowntimer source code.Reimplement it.

 /**
   * cancel the countdown.
   * /
  public synchronized final void cancel () {
    mcancelled=true;
    mhandler.removemessages (msg);
  }
 private static final int msg=1;
  //handles counting down
  private handler mhandler=new handler () {
    @override
    public void handlemessage (message msg) {
      synchronized (countdowntimer.this) {
        if (mcancelled) {
          return;
        }
        final long millisleft=mstoptimeinfuture-systemclock.elapsedrealtime ();
        if (millisleft<= 0) {
          onfinish ();
        } else if (millisleft<mcountdowninterval) {
          //no tick, just delay until done
          sendmessagedelayed (obtainmessage (msg), millisleft);
        } else {
          long lasttickstart=systemclock.elapsedrealtime ();
          ontick (millisleft);
          //take into account user "s ontick taking time to execute
          long delay=lasttickstart + mcountdowninterval-systemclock.elapsedrealtime ();
          //special case:user "s ontick took more than interval to
          //complete, skip to next interval
          while (delay<0) delay +=mcountdowninterval;
          sendmessagedelayed (obtainmessage (msg), delay);
        }
      }
    }
  };

As a result of android 5.0 and above

private boolean mcancelled=false;

So we only need to be in a system below 5.0,Remove

if (mcancelled) {
          return;
        }

Just remove this judgment.

  • Previous MySQL green version setting encoding and 1067 error explanation
  • Next RHEL65 Compile and Install MySQL5626 Tutorial