我正在尝试使用OnChronometerTickListener实现Chronometer

时间:2016-12-07 12:35:25

标签: java android chronometer

我基本上想在我的应用程序中使用秒表活动,它会在30秒后振动设备并在60秒时发出通知警报。我是应用程序开发的新手,所以请不要因为错过一个明显的答案而追捕我。

我知道我需要Chronometer.OnChronometerTickListener - 但不确定如何实施?

public void shotClockStart(View v) {

        Chronometer shotclock = (Chronometer) findViewById(R.id.chrono1);
        shotclock.start();
        long timeElapsed = SystemClock.elapsedRealtime() - shotclock.getBase();
        if (timeElapsed >= 30000) {
            //  HERE I WANT A VIBRATION ON THE DEVICE.
        }else if(timeElapsed>=60000){
            //HERE I WANT A NOTIFICATION ALERT 
        }
    }

1 个答案:

答案 0 :(得分:0)

您必须在Chronometer

上设置监听器
public void shotClockStart(View v) {
    Chronometer shotclock = (Chronometer) findViewById(R.id.chrono1);
    shotclock.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {
            long timeElapsed = SystemClock.elapsedRealtime() - chronometer.getBase();
            if (timeElapsed >= 30000) {
                //  HERE I WANT A VIBRATION ON THE DEVICE.
            }else if(timeElapsed>=60000){
                //HERE I WANT A NOTIFICATION ALERT
            }
        }
    });
    shotclock.setBase(SystemClock.elapsedRealtime());
    shotclock.start();
}