如何在countDownTimer完成之前等待应用程序

时间:2013-04-22 09:40:50

标签: android countdown

类似查询:wait until all threads finish their work in java

您好, 我正在开发包含某种倒计时器的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()第二个,第二个调用第一个调用,直到repeat = 0; 在决赛中,这对我来说是最好的选择。无论如何,谢谢你的帮助,@ Triode的答案对我有很大的帮助

2 个答案:

答案 0 :(得分:1)

SECOND, THIRD AND FOURTH COUNT DOWN TIMER

onFinish()中开始MyCountdownTimer

答案 1 :(得分:0)

我认为你必须放下 timerStop()方法实现,不要试图隐藏它,你需要回答但你不希望别人受益:s