20

Ive been looking to see a method to see if a CountDownTimer is running or not, but I cant find a way to, any help would be greatly appreciated

if (position == 0) {

    mCountDown = new CountDownTimer((300 * 1000), 1000) {

        public void onTick(long millisUntilFinished) {
            mTextField.setText("seconds remaining: "
                    + millisUntilFinished / 1000);
        }

        public void onFinish() {
            mTextField.setText("0:00");
            String path = "/sdcard/Music/ZenPing.mp3";
            try {

                mp.reset();
                mp.setDataSource(path);
                mp.prepare();
                mp.start();

            } catch (IOException e) {
                Log.v(getString(R.string.app_name),
                        e.getMessage());
            }
        }
    }.start();

}

For that how can I check if mCountDown is currently running?

Chintan Rathod
  • 24,674
  • 13
  • 76
  • 92
user3224105
  • 237
  • 1
  • 2
  • 7
  • Can't you use a global boolean? setting in to TRUE when calling new CountDownTimer and to FALSE un your onFinish() ? – Damien R. Jan 30 '14 at 08:41
  • can you elaborate what u wanna achieve???? – MGDroid Jan 30 '14 at 08:41
  • 2
    Notice, that using of the boolean variable in onTick() and onFinish() methods to indicate the status of the timer is not clear. As you can stop the timer by the cancel() method. And in this case, your variable will be still true and indicates wrong status. – Максим Петлюк May 04 '17 at 10:56

3 Answers3

53

Just put a boolean flag which indicate that by following code

boolean isRunning = false;

mCountDown = new CountDownTimer((300 * 1000), 1000) {

    public void onTick(long millisUntilFinished) {
        isRunning = true;
        //rest of code
    }

    public void onFinish() {
        isRunning= false;
        //rest of code
    }
}.start();
Chintan Rathod
  • 24,674
  • 13
  • 76
  • 92
  • 4
    This solution is not completely accurate, since calling mCountDown.cancel() does not trigger onFinish(), hence isRunning will be true, but the timer will not be running – Cris May 02 '19 at 11:34
  • as @Cris mentioned. you should call `isRunning = false` whenever you call `mCountDown.cancel()` – musooff Aug 16 '19 at 07:26
2

onTick is your callback for a running process, you can either set a property to track the status.

isTimerRunning =false;

After start -> make it true; Inside OnTick -> make it true (not actually required, but a double check) Inside OnFinish -> make it false;

use the isTimerRunning property to track the status.

duggu
  • 35,841
  • 11
  • 112
  • 110
gvmani
  • 1,380
  • 1
  • 11
  • 18
0

Check if the CountDownTimer is Running and also if the app is running in the background.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myButton.setText("Button clicked");
            countDownTimer = new CountDownTimer( 3000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    //After turning the Smartphone the follow both methods do not work anymore
                    if (!runningBackground) {
                        myButton.setText("Calc: " + millisUntilFinished / 1000);
                        myTextView.setText("Calc: " + millisUntilFinished / 1000);
                    }
                }
                @Override
                public void onFinish() {
                    if (!runningBackground) {
                        //Do something
                    }
                    mTextMessage.setText("DONE");
                    runningBackground = false;
                    running = false;
                }
            };
            //timer started
            countDownTimer.start();
            running = true;
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    runningBackground = false;
}

@Override
protected void onPause() {
    runningBackground = true;
    super.onPause();
}
AndroidStorm
  • 739
  • 7
  • 25