我打算用什么方法来启动倒数计时器" AccurateCountDownTimer"

时间:2013-12-06 06:10:12

标签: java android methods countdown countdowntimer

正如Sam的帖子所见 here,我不确定使用什么方法来启动倒数计时器“ AccurateCountDownTimer ”。

它将从一个按钮(onClick)调用,稍后可以轻松设置。 剩余时间将显示在文本视图中。

这是我的代码:

public abstract class AccurateCountDownTimer {

    public AccurateCountDownTimer(long millisInFuture, long countDownInterval) {
        long seconds = (millisInFuture / 1000);
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;

        // ************AccurateCountdownTimer***************
        mTickCounter = 0;
        // ************AccurateCountdownTimer***************
        testTimer.setText(String.format("%02d", seconds / 60) + ":"
                + String.format("%02d", seconds % 60));
    }

    /**
     * Cancel the countdown.
     */
    public final void cancel() {
        mHandler.removeMessages(MSG);
    }

    /**
     * Start the countdown.
     */
    public synchronized final AccurateCountDownTimer start() {
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }

        mNextTime = SystemClock.uptimeMillis();
        mStopTimeInFuture = mNextTime + mMillisInFuture;

        mNextTime += mCountdownInterval;
        mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG), mNextTime);
        return this;
    }

    /**
     * Callback fired on regular interval.
     * 
     * @param millisUntilFinished
     *            The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);{



    }

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();

    private static final int MSG = 1;

    // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            synchronized (AccurateCountDownTimer.this) {
                final long millisLeft = mStopTimeInFuture - SystemClock.uptimeMillis();

                if (millisLeft <= 0) {
                    onFinish();
                } else {
                    onTick(millisLeft);

                    // Calculate next tick by adding the countdown interval from the original start time
                    // If user's onTick() took too long, skip the intervals that were already missed
                    long currentTime = SystemClock.uptimeMillis();
                    do {
                        mNextTime += mCountdownInterval;
                    } while (currentTime > mNextTime);

                    // Make sure this interval doesn't exceed the stop time
                    if(mNextTime < mStopTimeInFuture)
                        sendMessageAtTime(obtainMessage(MSG), mNextTime);
                    else
                        sendMessageAtTime(obtainMessage(MSG), mStopTimeInFuture);
                }
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题...你问这个课程怎么用?我认为像这样启动计时器就足够了:

new AccurateCountDownTimer(millisInFuture, countDownInterval).start();

修改还可以进行source code中解释的回调:

new AccurateCountDownTimer(millisInFuture, countDownInterval) {

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

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