【问题标题】:How to wait application until countDownTimer finish如何等待应用程序直到 countDownTimer 完成
【发布时间】:2013-04-22 09:40:50
【问题描述】:

类似查询: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 的回答对我帮助很大

【问题讨论】:

    标签: android countdown


    【解决方案1】:

    在您的onFinish() 或您的MyCountdownTimer 中开始您的SECOND, THIRD AND FOURTH COUNT DOWN TIMER

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        相关资源
        最近更新 更多