我正在尝试使用 OnChronometerTickListener 实现一个 Chronometer
I am trying to implement a Chronometer with OnChronometerTickListener
我基本上想要一个秒表 activity 在我的应用程序中,它会在 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
}
}
您必须在 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();
}
我基本上想要一个秒表 activity 在我的应用程序中,它会在 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
}
}
您必须在 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();
}