An-droid
An-droid

Reputation: 6485

CountDownTimer value never goes to zero

I initialize the timer at 11000 ms and with log I see the timer going from 10959ms to 1484ms

private void startGameCountDown() {
        mCountDownTimer = new CountDownTimer(11000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            Log.i(TAG, "onTick: " + millisUntilFinished);

            mTimerTv.setText(String.format("%d", millisUntilFinished));
            mTimerTv.invalidate();
        }

        @Override
        public void onFinish() {
        }
    }.start();

What's wrong here ?

Upvotes: 0

Views: 106

Answers (2)

Keyur Lakhani
Keyur Lakhani

Reputation: 4371

You have just started with the creating object of CountDownTimer

Either you have to create like this:

private void startGameCountDown() {
    new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            Log.i(TAG, "onTick: " + millisUntilFinished);

            mTimerTv.setText(String.format("%d", millisUntilFinished));
            mTimerTv.invalidate();
        }

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

    }.start();
}

Or you can Create object and then you can start it like this

private void startGameCountDown() {

    CountDownTimer mCountDownTimer = new CountDownTimer(11000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            Log.i(TAG, "onTick: " + millisUntilFinished);

            mTimerTv.setText(String.format("%d", millisUntilFinished));
            mTimerTv.invalidate();
        }

        @Override
        public void onFinish() {
        }
    };
    mCountDownTimer.start();
}

Upvotes: 0

An-droid
An-droid

Reputation: 6485

I ended up creating my own timer :

public abstract class CountDownTimer {

    private static final String TAG = CountDownTimer.class.getSimpleName();

    private static final long TIMER_DEFAULT = 4000; //ms
    private static final long TIMER_STEP    = 1000; //ms

    private boolean mIsRunning = false;

    private long mRemainingTime = 0;
    private long mTotalTime;

    private Handler mHandler = new Handler();

/**
 * 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();

public CountDownTimer(long timerStartValue) {

    if (timerStartValue >= 1) {
        mTotalTime = timerStartValue * TIMER_STEP;
        mRemainingTime = timerStartValue * TIMER_STEP;
    } else {
        mTotalTime = TIMER_DEFAULT;
        mRemainingTime = TIMER_DEFAULT;
    }
}

public CountDownTimer start() {

    mIsRunning = true;

    // Start the initial runnable task by posting through the handler
    mHandler.postDelayed(runnableCode, TIMER_STEP);
    return this;
}

public void cancel() {
    mIsRunning = false;
    mHandler.removeCallbacks(runnableCode);
}

private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {

        if (mRemainingTime >= 0) {
            onTick(mRemainingTime);
        } else {
            mIsRunning = false;
            onFinish();
        }

        mRemainingTime -= TIMER_STEP;

        if (mIsRunning) {
            // Goes on
            mHandler.postDelayed(runnableCode, TIMER_STEP);
            }
        }
    };

}

Upvotes: 1

Related Questions