Question

I have the following CountDownTimer that helps me to set a button visible every 490 ms 30 seconds long.

Is it possible to say that the first 10 seconds the "tick" should be for example 1000ms, the next 10 seconds 700 ms and the last 10 seconds 490ms ?

new CountDownTimer(30000, 490) {

@Override
public void onTick(long millisUntilFinished) {
    for(int i = 0; i< arr.size(); i++){
        Button aga = arr.get(i);
        if(aga.getVisibility() == View.VISIBLE){
            aga.setVisibility(View.GONE);
        }
    }
    int zufall = (int) (Math.random()*23);
    setNextButton(arr.get(zufall));
}
Was it helpful?

Solution

Hope this will helpful.

public void countDown(long time) {
        if (time == 490) {
            return;
        }

        new CountDownTimer(10000, time) {

            public void onTick(long millisUntilFinished) {
                // Do whatever you want
             }

             public void onFinish() {
                 countDown(nextTime); // nextTime can be 700, 100, ... It's up to you. (Your rule). :">
             }

        }.start();

    }

OTHER TIPS

Aside from launching new timers at those intervals to handle the new requirement, your best bet would just be to find a common divisor and set that as your interval, then use a switch with modulus to act at your desired times.

Timer mTimer; // Global 

public void countDownTimerCaller()
{
     static int count = 0;

     int time;

    switch(count)
    {
    case 0:
        time = 1000;
        break;
    case 1:
        time = 700;
        break;
    case 2:
        time = 490;
        break;

        default:
                mTimer.cancel(); // stop timer
            return;
    }

       new CountDownTimer(10000, time) {

        @Override
        public void onTick(long millisUntilFinished) {
            for(int i = 0; i< arr.size(); i++){
                Button aga = arr.get(i);
                if(aga.getVisibility() == View.VISIBLE){
                    aga.setVisibility(View.GONE);
                }
            }
            int zufall = (int) (Math.random()*23);
            setNextButton(arr.get(zufall));

        }

    }

    count++;
}

mTimer = new Timer().schedule(countDownTimerCaller, 10000); // call from where ever you created CountDownTimer instance
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top