简体   繁体   中英

Extend time of an Countdowntimer

Why is this Code not working ?

This code should extend the Countdowntimer to 10 sec (everytime when point is 20 , 40 , 60) etc.But when I start it and my score is by 20 it shows in a TextView a right value of time.But then it is gone in 1 sec and the Countdowntimer got the old value and continue.Someone a idea ?

int bonus_time = 1 , sec = 10000 , point == 0; 

points_timer =new CountDownTimer(sec,1000) {

        @Override
        public void onTick(long millisUntilFinished) 
        {
                    if ( point == (bonus_time * 20))
                    {
                       ++bonus_time;
                       millisUntilFinished += 10000;
                       sec += 10000;
                    }

        ++point;
        }

        @Override
        public void onFinish() 
        {
          bonus_time = 1;
        }
    };

There is no way to extend a CountDownTimer, no method in the class provides this.

Your attempt to add time to sec is useless, I even wonder how it can compile. Anyway, in Java integers are passed by value, so once you passed 1000 , you passed it and can't change that value.

The only alernative is to re-create a new timer and use it instead of the old one. Or recode everything, a timer of your own that you can extend.

in

public void onTick(long millisUntilFinished) 

you do

millisUntilFinished += 10000;

you can not assign to primitive parameter and expect that it changes anything outside of that method (eg. scope of millisUntilFinished is onTick method and nothing more)

When you create a timer with the sec variable for its time, it does not magically connect the two so when the value of sec changes the timer's time changes, it just uses the value of sec once when you make the timer.

Same goes for changing millisUntilFinished , it's a parameter you got from a callback, by changing its value you are not doing anything.

The only way to change a timer's time is to create a new one. This is my suggestion:

// GLOBAL VARIABLES
CountDownTimer points_timer;
int bonus_time = 1 , sec = 10000 , point == 0; 

public void createTimer()
{
    points_timer =new CountDownTimer(sec,1000) {

        @Override
        public void onTick(long millisUntilFinished) 
        {
            sec = millisUntilFinished;

            if ( point == (bonus_time * 20))
            {
                ++bonus_time;
                //millisUntilFinished += 10000;
                sec += 10000;
                createTimer();
            }

            ++point;
        }

        @Override
        public void onFinish() 
        {
            bonus_time = 1;
        }
    };
}

The creation of the timer is surrounded by a function, and that function is called each time the bonus is gained to recreate the timer with the new value of sec .

  • You should also call that method once in place of your previous code, to initially create the timer.

Hope this works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM