0

I'm doing an app that displays various yoga poses for 60 seconds. I've been pulling my hair out trying to get a countdown timer to work the way I want. I need the timer to be able to pause, and restart automatically after 15 seconds. The CountDownTimer from Android didn't work because it had no pause. I tried 2 different versions of rewritten Android CountDownTimers and the classic version (Android CountDownTimer) and they all seem to have the same bug because they are using Handlers.

I've tried this version: http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm

I've tried this version: Android: CountDownTimer skips last onTick()! - I added an onFinish method to it.

I'm trying to do a Thread.sleep in the onFinish method for 15 seconds, and it is not updating the UI past the last second before moving into the Thread.sleep mode. In the following code, the TextView isn't getting updated to be set to empty string until after the Thread.sleep call.

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);
            }

        }
    };

Anyone have any ideas how to do this (restart after 15 seconds) differently?

Community
  • 1
  • 1
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106

1 Answers1

1

The onFinish method is being called from the interface thread, so any interface changes you make won't actually redraw until onFinish returns. Try something like this:

  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);
          }

      }
  };   
Jon Vance
  • 504
  • 3
  • 13