嗨,如何从变量启动android计时器

时间:2013-02-23 12:58:53

标签: android countdowntimer

使用了几个教程我设法创建了以下countdowntimer - 这让我为自己感到骄傲:) 现在我希望这个计时器能够从停止而不是从头开始再次启动时启动吗? 欢迎所有想法//链接/教程。

提前谢谢你。

    setTime(0);

}

private void setTime(int seconds) {
    // TODO Auto-generated method stub

    Time = seconds;
    int minutes = seconds / 60;
    seconds = seconds % 60;

    tvCzas.setText(String.format("%02d:%02d", minutes, seconds));

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == bAddSec)
        setTime(Time + 1);
    else if (v == bTakeSec)
        setTime(Time - 1);
    else if (v == bAddMinute)
        setTime(Time + 60);
    else if (v == bTakeMinute)
        setTime(Time - 60);
    else if (v == bReset)
        setTime(0);
    else if (v == bStart)
        if (mierzy)
            zatrzym();
        else
            rozpocznij();

}

private void rozpocznij() {
    // TODO Auto-generated method stub

    Timer = new CountDownTimer(Time * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int millis = (int) millisUntilFinished;
            int seconds = (int) millisUntilFinished / 1000;
            int minutes = seconds / 60;
            millis = millis % 100;
            seconds = seconds % 60;
            tvCzas.setText(String.format("%02d:%02d", minutes, seconds));
            bStart.setText("Stop");

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            mierzy = false;
            setCount(Licznik + 1);

            tvCzas.setText("Koniec!");
            bStart.setText("Start");
        }
    };

    Timer.start();
    bStart.setText("Stop");
    mierzy = true;
}

private void zatrzym() {
    // TODO Auto-generated method stub
    if (Timer != null)
        Timer.cancel();

    mierzy = false;
    bStart.setText("Start");

}

}

1 个答案:

答案 0 :(得分:0)

使用onSaveInstanceState和onRestoreInstanceState来存储您的计时器状态...请参阅此answer了解