尝试让CountDownTimer在15秒后重新启动。

时间:2013-01-24 22:30:35

标签: android restart countdowntimer

我正在做一个显示各种瑜伽姿势60秒的应用程序。我一直在试着让倒数计时器以我想要的方式工作。我需要计时器能够暂停,并在15秒后自动重启。来自Android的CountDownTimer不起作用,因为它没有暂停。我尝试了2个不同版本的重写Android CountDownTimers和经典版本(Android CountDownTimer),它们似乎都有相同的bug,因为它们使用的是Handler。

我试过这个版本:http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm

我试过这个版本:Android: CountDownTimer skips last onTick()! - 我添加了一个onFinish方法。

我正在尝试在onFinish方法中执行一个Thread.sleep 15秒,并且在进入Thread.sleep模式之前它没有更新UI超过最后一秒。在下面的代码中,在Thread.sleep调用之后,TextView不会更新为设置为空字符串。

cdt = new CountDownTimerWithPause(60000,1000,true) { 
        @Override
        public void onTick(long millisUntiFinished) { 
            tvClock.setText("Remaining:" + millisUntiFinished/1000);

        }

        @Override
        public void onFinish() {
            tvClock.setText("");
            bell.start();
            if (bAutoPlayIsOn) { 

                tvClock.setText("waiting for next pose...");
                try {

                    //15 second pauses between poses
                    Thread.sleep(15000);
                    //start over
                    listener.onForwardItemSelected();
                    ResetClocktoMaxTime(60000);
                    resume();
                } catch (InterruptedException e) {
                    Log.d("Sleep Interrupted", e.toString());
                    e.printStackTrace();
                }
                catch (Exception e) {
                    Log.d("Timer", e.toString());
                }
            }
            else { 
              tvClock.setText("Done");
                       btnStart.setText("Start ");
              bClockIsTicking = false;
              btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
            }

        }
    };

任何人有任何想法如何做到这一点(15秒后重启)不同?

1 个答案:

答案 0 :(得分:1)

正在从接口线程调用onFinish方法,因此在onFinish返回之前,您所做的任何接口更改都不会实际重绘。尝试这样的事情:

  cdt = new CountDownTimerWithPause(60000,1000,true) {
      @Override
      public void onTick(long millisUntiFinished) {
          tvClock.setText("Remaining:" + millisUntiFinished/1000);

      }

      @Override
      public void onFinish() {
          tvClock.setText("");
          bell.start();
          if (bAutoPlayIsOn) {

              tvClock.setText("waiting for next pose...");
              (
                new Thread() {
                    public void run() {
                        try {
                            //15 second pauses between poses
                            Thread.sleep(15000);
                            getActivity().runOnUiThread
                            (
                              new Runnable() {
                                  public void run() {
                                    //start over
                                    listener.onForwardItemSelected();
                                    ResetClocktoMaxTime(60000);
                                    resume();
                                  }
                              }
                            );
                        }
                        catch (InterruptedException e) {
                            Log.d("Sleep Interrupted", e.toString());
                            e.printStackTrace();
                        }
                        catch (Exception e) {
                            Log.d("Timer", e.toString());
                        }
                    }
                }

              ).start();

          }
          else {
            tvClock.setText("Done");
            btnStart.setText("Start ");
            bClockIsTicking = false;
            btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
          }

      }
  };