Xeo
Xeo

Reputation: 409

How to add time to a running countdown timer?

Before I start I have looked at lots of threads including:

How to add time to countdown timer? Android game countdown timer

But I just cant get my timer to work in the way I require. I want the timer to be counting down from say 30 and when and image is pressed (named imageview1 in this case) the timer adds 3 seconds to the timer to give it more time. I know you cannot essentially add the time while its running and you need to cancel and then start a new timer, The code I have so far is :

public void onClick(View v) {
    // TODO Auto-generated method stub
    //GlobalClass global = new GlobalClass();
    Random rand = new Random();

    CountDownTimer thetimer = new myTimer(millisInFuture, 1000);

    switch(v.getId()) {

    case R.id.buttonstart:
        btnstart.setVisibility(View.INVISIBLE);     
        thetimer.start();   
        break;

    case R.id.imageView1:       
        if (thetimer != null){
            thetimer.cancel();
            thetimer = new myTimer(countdownPeriod + 3000, 1000).start();

        }

        break;

with lots of other case references then :

public class myTimer extends CountDownTimer  {

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

        @Override
        public void onTick(long millisUntilFinished) {          
                timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
                countdownPeriod=millisUntilFinished;

        }

        @Override
        public void onFinish() {
            timedisplay.setText("Timer Finished");
            started = false;
            btnstart.setVisibility(View.VISIBLE);
        }
    }

I think the problem is its not cancelling the original timer so the label that shows the timer does some crazy things, like jumping around on different numbers both up and down as there would appear more than 1 class of thetimer. That is even though I have included the line thetimer.cancel(); The timer works fine if I just let it run to 0.

Any help would be great

Upvotes: 0

Views: 2906

Answers (1)

free3dom
free3dom

Reputation: 18978

You should not create your timer as a local in onClick. Instead create it as a global and start it somewhere else (in onCreate perhaps).

What happens with your current code is that whenever onClick is called a new timer is created and you then cancel the new timer - which has no effect on any previously created timer(s).

Try something like this:

public class MyActivity extends Activity  {

   CountDownTimer thetimer;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      thetimer = new myTimer(millisInFuture, 1000);
   }

   public void onClick(View v) {
      Random rand = new Random();
      switch(v.getId()) {
         case R.id.buttonstart:
            btnstart.setVisibility(View.INVISIBLE);     
            thetimer.start();   
            break;

         case R.id.imageView1:       
            if (thetimer != null) {
               thetimer.cancel();
               thetimer = new myTimer(countdownPeriod + 3000, 1000).start();
            }
            break;
      }   
   }    
}

You will still have to keep track of the global time somewhere - i.e. the countDonwPeriod used to re-create the timer instance when an image is touched - it should probably be extracted from the timer before canceling it.

Upvotes: 1

Related Questions