Android 如何在固定时间后暂停计时器
在我的应用程序中,我有一个计时器。我希望计时器在60秒后自动暂停。有人能帮我吗?这就是我所做的Android 如何在固定时间后暂停计时器,android,timer,Android,Timer,在我的应用程序中,我有一个计时器。我希望计时器在60秒后自动暂停。有人能帮我吗?这就是我所做的 private Runnable updateTimerThread = new Runnable() { public void run() { timeInMilliseconds = SystemClock.uptimeMillis() - startTime; updatedTime = timeSwapBuff + timeInMilliseco
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
/*timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));*/
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) );
customHandler.postDelayed(this, 0);
}
};
使用CountDownTimerwithPause类暂停和恢复计时器。参考这个 重写它的onTick方法。当timmer达到60000时,使用timer对象调用pause()。btn.setOnClickListener(新视图.OnClickListener()){ 受保护的void startCountDownTimer(){
您需要停止并记住旧值,并在需要时使用旧值重新运行。您不能暂停计时器,而是按照@Raghunandan所说的操作
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer();
}
});
cdTimer = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
//update total with the remaining time left
total = millisUntilFinished;
tv.setText("seconds remaining: " + millisUntilFinished/ 1000);
}
public void onFinish() {
tv.setText("done!");
}
}.start();
}