Checking if CountDownTimer is running

前端 未结 3 2025
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 21:36

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 =         


        
相关标签:
3条回答
  • 2021-01-03 22:05

    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();
    }
    
    0 讨论(0)
  • 2021-01-03 22:08

    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.

    0 讨论(0)
  • 2021-01-03 22:22

    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();
    
    0 讨论(0)
提交回复
热议问题