1
votes

I need to find a way to stop multiple timers from happening. I have to have a popup menu with selectable countdown timer intervals. I found a solution that works for creating the timers. However, if one is going, and you select another from the menu, they will both run together.

I need to figure out what code to add so that when a second countdown option is selected, the first countdown timer stops.

...

@Override
public boolean onMenuItemClick(MenuItem item) {

    switch (item.getItemId()){
        case R.id.countdownMenu1:
            CounterClass timer = new CounterClass(10000, 1000);
            timer.start();
            return true;

        case R.id.countdownMenu2:
            timer = new CounterClass(15000, 1000);
            timer.start();
            return true;

        default:
            return false;
    }
}


public class CounterClass extends CountDownTimer  {

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

    @Override
    public void onTick(long millisUntilFinished) {

        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);
    }

    @Override
    public void onFinish() {
        // textViewTime.setText("Completed.");
        System.exit(0);
    }
...

Selecting each time from the menu works great, as long as you don't try to second another time. Then I get two countdown times being displayed and which ever gets to zero first executes the onFinish.

1
I don't understand the problem. Cancel method don't works for you? As far as I understand, you just need cancel current timer if it's currently running. Is it? - kolombo
The problem is that I am not using a separate cancel button. I am only using the button that pops up the menu. If you pick 15 minutes from the menu it works fine. If you click on the menu again and select 10 minutes, now you have two timers trying to run at the same time. I thought there would be an easy check to turn off the old one before a new one starts, but maybe not. It is easy with a set time in the countdown timer. It resets every time. When I try to make a selectable durations, I run into problems. - Eric Patterson
So, you need: click on first menu item->its start timer for 15 minutes. After that, if click on 2 menu item -> its cancel first timer on 15 minutest and starts timer for 10 minutes. Im right? - kolombo
Right. I need to second timer option to cancel out the first timer option. It is relentless. I have even closed the activity and the timer still runs in the background until it is over. I have found no good way to cancel the timer in any of my setups. - Eric Patterson

1 Answers

0
votes
private CounterClass timer;

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.countdownMenu1:
            startTimer(10000, 1000);
            return true;
        case R.id.countdownMenu2:
            startTimer(15000, 1000);
            return true;
        default:
            return false;
    }
}

public void startTimer(long millisInFuture, long countDownInterval) {
    cancelTimer();
    timer = new CounterClass(millisInFuture, countDownInterval);
    timer.start();
}

private void cancelTimer() {
    if (timer != null) {
        timer.cancel();
    }
}