`counter.Cancel();` 在 `OnStop();` 方法中未被执行。

huangapple 未分类评论56阅读模式
英文:

counter.Cancel(); isn't executed in OnStop(); method

问题

我有一个 `CounterTimer`,当 API 调用成功时会启动如果用户按下 `KeyEvent.KEYCODE_HOME`,`CounterTimer` 必须取消但实际上它并没有取消而是继续运行

**这是我启动计时器的方式**

```java
private void countDownStart(String event_date_time) {
    String[] tokens = event_date_time.split(":");
    int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
    int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
    int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
    long total = secondsToMs + minutesToMs + hoursToMs;

    timer = new CountDownTimer(total, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) (millisUntilFinished / 1000) % 60;
            int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
            int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
            CountDown.setVisibility(View.VISIBLE);
            CountDown.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
        }

        public void onFinish() {
            CountDown.setVisibility(View.VISIBLE);
            CountDown.setText("00:00:00");
            loader.showProgressBar();
            startActivity(new Intent(mContext, Questions.class));
        }
    };
    timer.start();
}

这是我用来取消计时器的方法。我尝试过在 onStopKeyEvent.KEYCODE_HOME 中使用。

@Override
protected void onStop() {
    System.out.println("count: onStop Called");
    loader.hideProgressBar();
    if (timer != null) {
        timer.cancel();
    }
    super.onStop();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        loader.hideProgressBar();
        System.out.println("KEYCODE_HOME");
        if (timer != null) {
            timer.cancel();
        }
        return true;
    }
    return false;
}

可能有什么问题?


<details>
<summary>英文:</summary>

I have a `CounterTimer` which starts when an Api is successful. if a user presses the  `KeyEvent.KEYCODE_HOME` CounterTimer must cancel but instead of cancelling it keeps running

**This is how i start the counter**

    private void countDownStart(String event_date_time) {
    
            String[] tokens = event_date_time.split(&quot;:&quot;);
            int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
            int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
            int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
            long total = secondsToMs + minutesToMs + hoursToMs;
    
            timer = new CountDownTimer(total, 1000) {
                public void onTick(long millisUntilFinished) {
                    int seconds = (int) (millisUntilFinished / 1000) % 60;
                    int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
                    int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
                    CountDown.setVisibility(View.VISIBLE);
                    CountDown.setText(String.format(&quot;%02d:%02d:%02d&quot;, hours, minutes, seconds));
                }
    
                public void onFinish() {
    
                    CountDown.setVisibility(View.VISIBLE);
                    CountDown.setText(&quot;00:00:00&quot;);
                    loader.showProgressBar();
                    startActivity(new Intent(mContext, Questions.class));
     
                }
            };
            timer.start();
    
        }

**and this is what I used to cancel. I&#39;ve tried with both `onStop` and `KeyEvent.KEYCODE_HOME`**

 

     @Override
        protected void onStop() {
            System.out.println(&quot;count: onStop Called&quot;);
            loader.hideProgressBar();
            if (timer != null) {
                timer.cancel();
    
            }
            super.onStop();
        }

      @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_HOME)) {
                loader.hideProgressBar();
                System.out.println(&quot;KEYCODE_HOME&quot;);
                if (timer != null) {
                    timer.cancel();
                }
                return true;
            }
            return false;
        }

What could be possibly wrong here?

</details>


huangapple
  • 本文由 发表于 2020年5月4日 16:59:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/61588516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定