我读到没有直接的方法可以重新开始(或更新)时间。我试着做:
Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000, importantMessage, showTimerMessage);
Const.counter.start();
每次我希望计时器重新启动但它不起作用(旧计时器仍然存在)。我能做些什么?
编辑:我也试试这个
if(Const.counter != null){
Const.counter.cancel();
Const.counter.start();
}
else{
Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000, importantMessage, showTimerMessage);
Const.counter.start();
}
但它是暂停和恢复而不是重新开始
编辑2:也许我会写更多我的代码:
package com.example.fishe;
import android.os.CountDownTimer;
//countdowntimer is an abstract class, so extend it and fill in methods
public class CustomTimerTask extends CountDownTimer{
private Messages gameoverMes;
private Messages showRemailTimeMes;
public CustomTimerTask(long millisInFuture, long countDownInterval , Messages gameoverMes , Messages showRemailTimeMes) {
super(millisInFuture, countDownInterval);
this.gameoverMes = gameoverMes;
this.showRemailTimeMes =showRemailTimeMes;
}
@Override
public void onFinish() {
gameoverMes.setMessage("GAME OVER");
showRemailTimeMes.setMessage(" " + 0);
Game.gameOver();
}
@Override
public void onTick(long millisUntilFinished) {
Const.secUntilFinished = millisUntilFinished/1000;
if( Const.secUntilFinished == 6)
GameSounds.play(Const.TIME_GOING_TO_OVER,false);
showRemailTimeMes.setMessage(" " + Const.secUntilFinished);
}
}
在我定义的第一个计数器完成后,我总是在 showRemailTimeMes 中看到“0”,尽管我再次开始计数。
@Override
public void onResume()
{
super.onResume();
if(Const.counter != null){
Const.counter = null;
}
Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000,
importantMessage, showTimerMessage);
Const.counter.start();
}