Timer和TimerTask实现计时

Timer timer = new Timer();
mTime = 15;
    time_tv.setText(String.valueOf(mTime));
    mTimerTask = new TimerTask() {
      @Override public void run() {
        runOnUiThread(new Runnable() {
          @Override public void run() {
            mTime--;
            time_tv.setText(String.valueOf(mTime));
            if (mTime == 0) {
              manager.stopDetector();
              cancelTask();
              toFailedActivity("检测超时");
            }
          }
        });
      }
    };
    timer.schedule(mTimerTask, 1000, 1000);
 private void cancelTask() {
    if (mTimerTask != null && timer != null) {
      if (!mTimerTask.cancel()) {
        timer.cancel();
        mTimerTask.cancel();
      }
    }
  }

你可能感兴趣的:(Timer和TimerTask实现计时)