Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple CountDown Timer running one after another

this is my first question here. I need to implement in my app six countdown timers running one after another. When first finishes, next one starts and so on. Init time of every single run depends on user input. The problem is that I need to put different code in onTick() and onFinish() methods for every single countdown timer running and, well, I'm not sure how to start a next counter after one is finished. I was thinking about calling next counter in onFinish() method of current one but I can't figure out how to do this with 6 counters.

This is my Countdown timer class:

public class Counter extends CountDownTimer
{

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

    public void onTick(long millisUntilFinished)
    {
            //this code is the same for every counter
            timer_view.setText(formatTime(millisUntilFinished));

            //this code depends on specific counter running
            output_view1.setText("My output here");
            output_view2.setText("My output here");
            output_view3.setText("My output here");

    }

    public void onFinish()
    {
        playSound(sound_id_1);
        runMyMethod(user_input_1);

        timerHasStarted = false;


    }

}

I'm starting my counter in the same activity:

if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
    counter.start();
}
like image 206
elhm Avatar asked Dec 01 '25 01:12

elhm


1 Answers

You probably need to break out the start timer functionallity and call it in onFinish().

    public void startTimer(int counterId){
            Counter counter = null;
            switch(counterId){
                    case 0:
                            counter = new CounterOne(counterId,user_input1,1000);
                            break;  
                    /* Counter 1-5  goes here*/
                    default:
                            break;
            }
            if(counter !=null ){
                    counter.start();
            }
    }

then start your next timer in onFinsh()

    public abstract class Counter extends CountDownTimer
    {
            private int counterId;

            public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
            {
                    super(millisInFuture, countDownInterval);
                    this.counterId = counterId;
            }

            public abstract void onTick(long millisUntilFinished);

            public void onFinish()
            {
                    playSound(sound_id_1);
                    runMyMethod(user_input_1);
                    startTimer(this.counterId++);
            }

    }

    public class CounterOne extends Counter{
            public void onTick(long millisUntilFinished)
            {
                    //counter 1 logic
            }
    }

    /* Subclass others. eg. CounterTwo etc. */
like image 66
ejohansson Avatar answered Dec 03 '25 18:12

ejohansson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!