0

as seen in Sam's post on here, I am unsure what method to use to start the countdown timer "AccurateCountDownTimer".

It will be called from a button (onClick) which is easily set up later. And the Time remaining will be displayed on a text view.

Here is my code:

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);
                }
            }
        }
    };
}
Community
  • 1
  • 1
HairySocks
  • 13
  • 2

1 Answers1

0

I am not sure if i understand your question... Do you ask how to use this class? I think it will be enough to start timer like this:

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

Edit: And also you can have callbacks like explained in source code:

new AccurateCountDownTimer(millisInFuture, countDownInterval) {

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

  public void onFinish() {
      mTextField.setText("done!");
  }
}.start();
yahya
  • 4,810
  • 3
  • 41
  • 58
  • I want to have the timer started from a button like this: public void onClick(View v) { if (v.getId() == R.id.buttonStartR1) { //start timer here } In the same way that if I was calling a regular countDownTimer I would use countDownTimer(); – HairySocks Dec 06 '13 at 06:18
  • When I use that code added it gives the error: "Cannot instantiate the type MainActivity.AccurateCountDownTimer" – HairySocks Dec 06 '13 at 06:21
  • Try creating a public class instead of inner class – yahya Dec 06 '13 at 06:22
  • The idea of this countdown timer is to create a more accurate one than the specified countdown timer method as supplied by android. I have already successfully implemented the regular countdown timer but need something more accurate which does not skip ticks. – HairySocks Dec 06 '13 at 14:18
  • Then your question was not supposed to be "what method to use to start". This is a different case. – yahya Dec 06 '13 at 17:26
  • Well it was, You obviously didn't read my original question or the thread I linked correctly. All I want is to know what I need to use to start the accurate timer as stated in the code above. – HairySocks Dec 07 '13 at 11:52