倒计时器不能正常工作

时间:2015-01-27 03:58:25

标签: android

我正在使用倒数计时器来计算时间,并且它不能顺利计数多次。

timer = new CountDownTimer(new Long(leftOverTime), 1000) {
  public void onTick(long millisUntilFinished) {
    tv_workout_time.setText(Long.toString(millisUntilFinished / 1000));
    leftOverTime = millisUntilFinished;
  }

  public void onFinish() {
    newWorkoutMethod();
  }
}.start();

1 个答案:

答案 0 :(得分:1)

尝试我的代码,我认为您需要打破启动计时器功能并使用onFinish()方法调用它。

 public void startTimer(int countersId){
 Counter counter = null;
 switch(countersId){
 case 0:
 counter = new CounterOne(countersId,your_input1,1000);
 break;  
 /* Counter 1 to 3  goes here*/
 default:
 break;
 }
 if(counter !=null ){
 counter.start();
 }
 }

并使用onFinsh()方法拨打下一个计时器。

 public abstract class Counter extends CountDownTimer
 {
 private int countersId;

 public Counter(int countersId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
  {
 super(millisInFuture, countDownInterval);
 this.countersId = countersId;
 }
 public abstract void onTick(long millisUntilFinished);
 public void onFinish()
 {
  playMusic(song_1);
  runTask(your_input_1);
  startTimer(this.countersId++);
  }
  }

public class CounterOne extends Counter{
public void onTick(long millisUntilFinished)
{
                //counter 1 logic
 }
}