hyperloris
hyperloris

Reputation: 346

How to add and remove time on CountDownTimer?

The question is: How can I add or remove time from the CountDownTimer during the count down?

For example: The user does something good: +5sec, the user does something wrong: -5sec.

Can someone help me with some code?

Upvotes: 2

Views: 4256

Answers (3)

murali kurapati
murali kurapati

Reputation: 1586

private class startTimer extends CountDownTimer
{

    public startTimer(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        currenttime=millisUntilFinished;
        textview.setText("" + currenttime / 1000);
    }

    @Override
    public void onFinish() {
        textview.setText("done");
    }
}

The above class is a simple derived class of CountDownTimer

CountDownTimer timer = new CountDownTimer(30000,1000);
timer.start();
long currentTime;

For example the above timer starts from 30 secs,decreases by one for each second. To increase or decrease the timer dynamically you can cancel the old timer and initialize with your new time as shown below

 timer.cancel();
 timer = new CountDownTimer(currentTime+5000,1000);// +5000 to increase by 5 secs
 timer.start();

you can make timer,currentTime variables as global then you can use the two different parts of code at different location.

Upvotes: 1

Mark
Mark

Reputation: 7615

You could restart the timer every time the user changes:

class Timer {
    private long remainingTime;
    private CoundDownTimer timer;

    public void addTime(long addedTimeInMillis) {
      createNewTimer(remainingTime + addedTimeInMills);
    }

    public void createNewTimer(long timeInMillis) {
       if(timer != null) {
         timer.cancel();
       }
       timer = new CountDownTimer(timeInMillis, 1000) {

       @Override
       public void onTick(final long millisUntilFinished) {
           remainingTime = millisUntilFinished;
       }

       @Override
       public void onFinish() {
           // do something here
       }
       }.start();

    }
}

Upvotes: 1

Alexander
Alexander

Reputation: 48232

Maybe something like this

abstract class MyTimer {

    public MyTimer(long deadline, long interval)
    {
        mDeadline = deadline;
        mInterval = interval; 

        mTimer = new MyCountDownTimer(mDeadline, mInterval);

    }

    public synchronized void start() {

       mTimer.start();

    }

    public abstract void onTick(long time);
    public abstract void onFinish();

    public synchronized void userDidRight() 
    {
        mTimer.cancel();
        mTimer = new MyCountDownTimer(mDeadline, mInterval += 5000);
        mTimer.start();
    }

    public synchronized void userDidWrong() 
    {
        mTimer.cancel();
        mTimer = new MyCountDownTimer(mDeadline, mInterfval -= 5000);
        mTimer.start();
    }


    private class MyCountDownTimer extends CountDownTimer() {

        private abstract void onFinish() {
            MyTimer.this.onFinish();
        }

        private abstract void onTick(long time) {
            MyTimer.this.onTick(time);
        }
    }

    private MyCountDownTimer mTimer;
}

Upvotes: 6

Related Questions