2013-02-15 2 views
0

В основном я делаю функцию кардио и имею три таймера обратного отсчета подряд, вложенные друг в друга, поэтому, когда один таймер заканчивается, начинается следующий. Один для времени подготовки, один для времени тренировки и один для времени отдыха, пользователь выбирает время.Таймер обратного отсчета не зацикливается

Мне нужно, чтобы он зацикливался, но много раз пользователь выбирает из numberpicker, но независимо от того, что я делаю, он проходит один раз один раз и не зацикливается, поэтому я знаю, что все это работает, это всего лишь часть цикла, т работы.

Я что-то упустил? Есть лучший способ сделать это?

//Main countdown timers loop 
    for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker 
    { 
     prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) { 

      public void onTick(long millisUntilFinished) { 

       tvRoundCount.setText("Round " + roundCount + "/" + times.getValue()); 
       tvCountDown.setText((millisUntilFinished/1000) + "s"); 
       if(millisUntilFinished <= (6 * 1000)) 
       { 
        tvCountDown.setTextColor(Color.RED); 
       } 
      } 

      public void onFinish() { 
       workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) { 

        public void onTick(long millisUntilFinished) { 
         tvCountDown.setTextColor(Color.GREEN); 
         tvCountDown.setText((millisUntilFinished/1000) + "s"); 
         if(millisUntilFinished <= 6 * 1000) 
         { 
          tvCountDown.setTextColor(Color.RED); 
         } 
        } 

        public void onFinish() { 
         restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) { 

          public void onTick(long millisUntilFinished) { 
           tvCountDown.setTextColor(Color.GREEN); 
           tvCountDown.setText((millisUntilFinished/1000) + "s"); 
           if(millisUntilFinished <= 6 * 1000) 
           { 
            tvCountDown.setTextColor(Color.RED); 
           } 
          } 

          public void onFinish() { 
           roundCount = roundCount + 1; 
          } 
          }.start(); 
        } 
        }.start(); 
      } 
      }.start(); 

    } 

ответ

0

проблема заключается в том, что вы создаете prepCountTimer и назначаете на законченный ect, затем запустите его. затем он достигает конца для каждого, и петли снова делают еще один preopCountTimer и начинают его. вам нужно сделать свой restCountTimer стартом следующего preopCountTimer, как только это будет сделано. если я не понимаю что-то не так.

public void callingMethod() { 
    timerMethod(times.getValue()); 
    // execution continues as your timer will run in a different thread 
} 

public void timerMethod(final int count) { 
    if (count == 0) { 
     // we have done the number of timers we want we can 
     // call whatever we wanted to once our timers were done 
    } 
    //you could use count to get the times for each timer here 
    startTimer(_finalPrep, new timerListner() { 
     @Override 
     public void timerFinish() { 
      //when timer 1 finishes we will start timer 2 
      startTimer(_finalWorkout, new timerListner() { 
       @Override 
       public void timerFinish() { 
        //when timer 2 finishes we will start timer 3 
        startTimer(_finalRest, new timerListner() { 
         @Override 
         public void timerFinish() { 
          //when timer 3 finishes we want to call the next timer in the list. 
          timerMethod(count - 1); 
         } 
        }); 
       } 
      }); 
     } 
    }); 
} 

private interface timerListner { 
    void timerFinish(); 
} 

public void startTimer(int timerTime, final timerListner onFinish) { 
    // you can pass in other parameters unqiue to each timer to this method aswell 
    CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) { 
     public void onTick(long millisUntilFinished) { 
      tvRoundCount.setText("Round " + roundCount + "/" + times.getValue()); 
      tvCountDown.setText((millisUntilFinished/1000) + "s"); 
      if (millisUntilFinished <= (6 * 1000)) { 
       tvCountDown.setTextColor(Color.RED); 
      } 
     } 

     @Override 
     public void onFinish() { 
      onFinish.timerFinish(); 
     } 
    }; 
    timer.start(); 

} 
+0

Как бы это сделать? Я не понимаю, почему это не будет просто повторяться x раз в соответствии с тем, что выбрал пользователь ... – user1875797

+0

Я отредактировал свой ответ, чтобы показать, как вы можете это сделать. – Eluvatar