18

I'm extending the CountDownTimer class to obtain some custom functionality .In onTick() in case some conditions are met I call cancel() , expecting that will be the end of it, however the onTick() callback gets call until the the count down is reached . So how to prevent this from happening ?

rantravee
  • 7,411
  • 9
  • 35
  • 47

3 Answers3

18

CountDownTimer.cancel() method seems to be not working. Here's another thread without a solution Timer does not stop in android.

I would recommend you to use Timer instead. It's much more flexible and can be cancelled at any time. It may be something like that:

public class MainActivity extends Activity {    
    TextView mTextField;
    long elapsed;
    final static long INTERVAL=1000;
    final static long TIMEOUT=5000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextField=(TextView)findViewById(R.id.textview1);

        TimerTask task=new TimerTask(){
            @Override
            public void run() {
                elapsed+=INTERVAL;
                if(elapsed>=TIMEOUT){
                    this.cancel();
                    displayText("finished");
                    return;
                }
                //if(some other conditions)
                //   this.cancel();
                displayText("seconds elapsed: " + elapsed / 1000);
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
    }

    private void displayText(final String text){
        this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                mTextField.setText(text);
            }});
    }
}
Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • 4
    Just made a drop-in replacement for CountDownTimer that you can cancel from within onTick : https://gist.github.com/737759 – Gautier Hayoun Dec 12 '10 at 01:04
  • @GautierHayoun By you method I can cancel in onTick, but the onTick() will still be called, don't know why – Deqing May 18 '14 at 03:44
16

CountDownTimer is also working fine for me, but I think it only works if you call it OUTSIDE of the CountDownTimer implemetation (that is don't call it in the onTick).

Calling it inside also didn't worked.

maid450
  • 7,478
  • 3
  • 37
  • 32
  • 3
    Yea, calling it in onTick doesnt seem to work.I forcely called onFinish and tried cancelling the countdowntimer inside onFinish and it worked – Basher51 Dec 24 '14 at 08:08
0

I tried this code snippet, since most answers are saying you cannot cancel the timer inside its implementation, thus i tried using a handler inside onFinish. Old post but if anyone comes across this its helpful.

new Handler().post(new Runnable() {
            @Override
            public void run() {
                timerTextView.setText("00:" + String.format("%02d", counter));
                cancel();
            }
        });