jle
jle

Reputation: 696

android - countdown timer is not canceling

In my app the user gets a point, when he clicks a button within 5 seconds. After that the timer should be canceled and restart. The problem I have is that the timer countinues counting down until it restarts. Is it set up wrong, or doesn't cancel(); stop the timer at all?

public class MainActivity extends AppCompatActivity {

    Button btn;
    TextView text;
    TextView scoretv;
    private static final String FORMAT = "%02d:%02d";
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scoretv = (TextView) findViewById(R.id.textView3);
        btn = (Button) findViewById(R.id.button1);
        text = (TextView) findViewById(R.id.textView2);
    }


    public void onClick(View view) {
        score++;
        scoretv.setText(String.valueOf(score));
        load();
    }

    private void load() {
        // TODO Auto-generated method stub

        new CountDownTimer(5000, 10) { // adjust the milli
            // seconds here
            public void onTick(long millisUntilFinished) {
                text.setText(""
                        + String.format(
                        "%02d:%03d",
                        TimeUnit.MILLISECONDS
                                .toSeconds(millisUntilFinished)
                                - TimeUnit.MINUTES
                                .toSeconds(TimeUnit.MILLISECONDS
                                        .toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS
                                .toMillis(millisUntilFinished)
                                - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS
                                .toSeconds(millisUntilFinished))));

            }

            public void onFinish() {
                text.setText("GameOver.");
                cancel();
            }
        }.start();
    }
}

Upvotes: 0

Views: 1612

Answers (2)

Chandana Gihan Perera
Chandana Gihan Perera

Reputation: 51

Define variable

private final long timeLeftInMillis=60000;

Create class

public void startCountDown() {
    countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            //Edit text set with time remaining
            int seconds = (int) (millisUntilFinished / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            etime.setText(  String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));



        }
        @Override
        public void onFinish() {


        }
    }.start();
}

I used this in a quiz app to reset timer when an answer is given. Therefore I called timer from the class that I used to add a new question.

private void newQuestion(){
    if (countDownTimer!=null){
        countDownTimer.cancel();
    }

getNextQuestion();
}

Upvotes: 0

Chris Gong
Chris Gong

Reputation: 8229

Because you're calling cancel() in onFinish, the timer won't stop when the user clicks the button. What will happen instead is that the button will start a 5 second CountDownTimer and at the end of the timer, cancel() will be called. But what's the point of cancelling a timer when it's already finished?

To fix this, I'd suggest making a global instance of a CountDownTimer object, instantiate it in the onCreate method, and cancel it in the onClick method.

First, add this to your global scope,

CountDownTimer timer;

Then, add what you originally had before in the load method to your onCreate,

timer = new CountDownTimer(5000, 10) { // adjust the milli
        // seconds here
        public void onTick(long millisUntilFinished) {
            text.setText(""
                    + String.format(
                    "%02d:%03d",
                    TimeUnit.MILLISECONDS
                            .toSeconds(millisUntilFinished)
                            - TimeUnit.MINUTES
                            .toSeconds(TimeUnit.MILLISECONDS
                                    .toMinutes(millisUntilFinished)),
                    TimeUnit.MILLISECONDS
                            .toMillis(millisUntilFinished)
                            - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS
                            .toSeconds(millisUntilFinished))));

        }

        public void onFinish() {
            text.setText("GameOver.");
            //cancel(); <-this is redundant
        }
    }.start();

And call timer.cancel() in your onClick method,

public void onClick(View view) {
    score++;
    scoretv.setText(String.valueOf(score));
    //load(); <-unnecessary
    timer.cancel();
}

Lastly, I'd suggest getting rid of load since it's sort of unnecessary at this point.

Upvotes: 3

Related Questions