Question

I have one CountDownTimer and i want to do an action for every two wasted seconds.

countdowntimer = new CountDownTimer(10000, 1) {

            @Override
            public void onTick(long millisUntilFinished) {


            }

            @Override
            public void onFinish() {




            }
        }.start();

For example when it starts time remaining = 10 seconds

When time remaining = 8 seconds I want to do an action

When time remaining = 6 seconds I want to do an action

and so on......

Was it helpful?

Solution

Just check if it is divisible by 2.

@Override
public void onTick(long millisUntilFinished) {
      long seconds = millisUntilFinished/1000;
      if ((seconds % 2) == 0)
      {     // even number--do some action  }
}

This is assuming you are calling the onTick() every second like

countdowntimer = new CountDownTimer(10000, 1000) {

This is probably preferable for you

Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often

countdowntimer = new CountDownTimer(10000, 2000) {

then you could just do the action with every call to onTick()

CountDownTimer Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top