Handler#removeCallbacksAndMessages 无法正常工作

Handler#removeCallbacksAndMessages not working properly

我创建了一个 h.postdelayed,如果条件为真,我想取消它。 我写了 if 条件并在里面做了 h.removeCallbacksAndMessages(null)。但是,我仍然可以看到 "timer" 运行。有帮助吗?

h.postDelayed(new Runnable() {
  public void run() {
    timePassed++;
    timeLeft = (maxTime - timePassed) / 10;
    timeLeftStr = "Time left: " + timeLeft + " seconds";
    timer.setText(timeLeftStr);

    if (timeLeft <= 0) {
      started = false;

      h.removeCallbacksAndMessages(null);

      setupGameOver(restartBtn, header1, header2, timer);
    }

    h.postDelayed(this, 100);
  }
}, 100);

如果不想再发运行,请不要延迟发帖

h.postDelayed(new Runnable() {
    public void run() {
        timePassed++;
        timeLeft = (maxTime - timePassed) / 10;
        timeLeftStr = "Time left: " + timeLeft + " seconds";
        timer.setText(timeLeftStr);

        if (timeLeft <= 0) {
            started = false;

            h.removeCallbacksAndMessages(null);

            setupGameOver(restartBtn, header1, header2, timer);
        } else {
            // run again.
            h.postDelayed(this, 100);
        }

    }
}, 100);