我有一个包含计时器的项目,我试图在达到12分钟的时间时发出警报或播放音乐。这怎么可能呢?如果有人可以帮助我,我是一个初学者。
用于启动,暂停和重置计时器的按钮
switch (view.getId()) {
case R.id.bt_control:
if (btControl.getText().equals("Play")) {
this.startService(intent);
cmPasstime.setBase(SystemClock.elapsedRealtime());
cmPasstime.start();
btControl.setText("Stop");
} else if (btControl.getText().equals("Stop")) {
this.stopService(intent);
cmPasstime.stop();
btControl.setText("Play");
} else if (btControl.getText().equals("Play")) {
this.startService(intent);
cmPasstime.start();
btControl.setText("Stop");
}
break;
case R.id.bt_reset:
reset();
break;
}
秒表
@Override
public void onChronometerTick(Chronometer arg0) {
seconds++;
cmPasstime.setText(formatseconds());
}
public String formatseconds() {
String hh = seconds / 3600 > 9 ? seconds / 3600 + "" : "0" + seconds
/ 3600;
String mm = (seconds % 3600) / 60 > 9 ? (seconds % 3600) / 60 + ""
: "0" + (seconds % 3600) / 60;
String ss = (seconds % 3600) % 60 > 9 ? (seconds % 3600) % 60 + ""
: "0" + (seconds % 3600) % 60;
return hh + " : " + mm + " : " + ss;
}
重置
private void reset() {
tvPasstime.setText("00:00:00");
cmPasstime.setBase(SystemClock.elapsedRealtime());
cmPasstime.stop();
btControl.setText("Play");
}
答案 0 :(得分:0)
尝试使用Firebase JobDispatcher,此库将在后台安排作业在指定时间执行某项操作。 https://github.com/firebase/firebase-jobdispatcher-android