2013-04-22 60 views
0

類似的查詢:wait until all threads finish their work in java如何等待應用程序,直到countDownTimer完成

你好, 我開發一個包含某種倒計時器的android應用。我的問題是,我有多個倒計時定時器,用於設置TextView的文本,並有單獨工作(第一倒計時定時器要等到第二個完成等)

見代碼:

MyCountDownTimer.java

public class MyCountdownTimer extends CountDownTimer { 
TextView tv; 

    // default constructor 
    public MyCountdownTimer (long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    // constructor 
    public MyCountdownTimer(int hours, int mins, int secs, 
    long countDownInterval, TextView tv) { 
     super(3600000 * hours + 60000 * mins + secs * 1000, countDownInterval); 
     this.tv = tv; 
      // all other settings 
    } 

    // when finished, set text of textview to done 
    @Override 
    public void onFinish() { 
     tv.setText("done!"); 
    } 

    // when working periodically update text of text view to value of time left 
    @Override 
    public void onTick(long millisUntilFinished) { 
tv.setText(/*function setting text of TextView based on time left*/); 
    } 
} 

Timer.java

public class Timer extends Fragment { 
Button bStart; 
Button bStop; 
TextView tvShowTime; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.timer, container, false); 
} 

public void onStart() { 
    super.onStart(); 
    bStart = (Button) getView().findViewById(R.id.bTimerStart); 
    bStop = (Button) getView().findViewById(R.id.bTimerStop); 
    tvShowTime = (TextView) getView().findViewById(R.id.showTime); 
    // setting on button start click 
    bStart.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      timerStart(); 
     } 
    }); 

    // setting on button stop click 
    bStop.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      timerStop(); 
     } 
    }); 
} 
    private void timerStart() { 
     bStart.setClickable(false); 
     int repeat = 2; 
     int hour, mins, secs; 
     for (int i = 0; i < 2 * repeat; i++) { 
      if (i % 2 == 0) { 
       // setting working count down timer values 
       mins = 1; 

      } else { 
       // setting resting count down timer values 
       secs = 30; 
        } 
      timerCount = new MyCountdownTimer(hours, mins, secs, REFRESH_RATE, 
      tvShowTime); 
      timerCount.start(); 
      ////////////////////////////////////////////////// 
      // HERE I WANT TO WAIT UNTIL COUNTDOWN IS DONE // 
      // ATM SECOND, THIRD AND FOURTH COUNT DOWN // 
      //   TIMER IS STARTED     // 
      ////////////////////////////////////////////////// 
     } 
} 

- 編輯 -

在我到底2個CountDownTimers其中onFinish()第二個和第二個呼叫的第一個重複直到= 0第一個呼叫; 在最後它是我最好的選擇。不管怎樣感謝您的幫助,@三極管的回答對我幫助很大

+0

這有幫助嗎? – Triode 2013-04-23 10:14:03

回答

1

在你MyCountdownTimer

0

我認爲你必須放下timerStop()方法實現,也不要試圖掩蓋它onFinish()啓動SECOND, THIRD AND FOURTH COUNT DOWN TIMER ,你需要回答,但你不想讓別人受益:s