Verifying CountDownTimer Execution

I was looking for a way to see if CountDownTimer is working or not, but I can not find a way, 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(); } 

To do this, how can I check if mCountDown is running?

+8
android timer countdowntimer
source share
3 answers

Just set the boolean flag, which indicates that the 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(); 
+29
source share

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

 isTimerRunning =false; 

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

use the isTimerRunning property to track state.

+1
source share

Check if CountDownTimer is running, or if the application 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(); } 
0
source share

All Articles