Android Chronometer倒计时完成

时间:2019-03-11 12:11:00

标签: java android xml timer countdowntimer

我正在尝试在我的白噪声应用中实现计时器。

应用程序具有4个用于设置不同音调的按钮和4个功能按钮:播放/暂停,停止,一个小时和三个小时。最后两个正在倒数计时模式下运行。

private Chronometer chronometer;
private boolean running;
private long pauseOffset;

switch (v.getId()) {
        case R.id.play_button:
            if (choosenAudio.isPlaying()) {
                //logic here
                pauseChronometer();
                break;
            }
            else {
                //logic here
                startChronometer();
                break;
            }

        case R.id.stop_button:
            //logic here
            resetChronometer();
            chronometer.setCountDown(false);
            chronometer.stop();
            break;

        case R.id.one_hour:
            //logic here
            chronometer.setCountDown(true);
            chronometer.setBase(SystemClock.elapsedRealtime() + 3600000);
            pauseOffset = 0;
            chronometer.start();
            break;

        case R.id.three_hours:
            //logic here
            chronometer.setCountDown(true);
            chronometer.setBase(SystemClock.elapsedRealtime() + 10800000);
            pauseOffset = 0;
            chronometer.start();
            break;

        default:
            break;
    }

我的方法:

public void startChronometer() {
    if (!running) {chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
        chronometer.start();
        running = true;
    }

}

public void pauseChronometer() {
    if (running) {
        chronometer.stop();
        pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
        running = false;
    }

}

public void resetChronometer() {
    chronometer.setBase(SystemClock.elapsedRealtime());
    pauseOffset = 0;

}

我的问题:

当倒计时时间达到00:00时,我如何检测并制作某些东西(即停止播放音乐)?

谢谢大家!

1 个答案:

答案 0 :(得分:0)

使用必须像这样使用setOnChronometerTickListener侦听器

chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
          @Override
          public void onChronometerTick(Chronometer chronometer) {
              long time = chronometer.getBase();
              //Use this time for finding minute and seconds
           }
     });