在我的应用程序中我正在使用CountDownTimer,并希望用户能够重置应用程序中的数据并重新开始。我遇到的问题是,如果你开始计时器,比如3分钟,然后很短的时间后你调用我的重置功能将所有分数设置回初始值,然后开始一个新的计时器,比如10分钟。 ...计时器立即从中断的地方继续说2:24,2:23,2:22 ......
我到处读到的所有内容都说你叫countdowntimername.cancel();然后设置countdowntimername = null;
这就是我这样做的方式,而这根本不起作用。必须有更有效的方法来杀死这些计时器?有什么想法吗?
调料:
private void toggleTimer() {
if (timerStarted == false) {
final CountDownTimer countDownTimer = new CountDownTimer(startTime, interval) {
public void onTick(long millisUntilFinished) {
if (timerPaused == true) {
cancel();
} else {
timeRemaining = millisUntilFinished;
long remainder = millisUntilFinished;
minuteTens = df.format(millisUntilFinished / 600000);
remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
minuteOnes = df.format(remainder / 60000);
remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
secondTens = df.format(remainder / 10000);
remainder = remainder - (Integer.valueOf(secondTens) * 10000);
secondOnes = df.format(remainder / 1000);
setCountdownText();
}
}
public void onFinish() {
minuteTens = "0";
minuteOnes = "0";
secondTens = "0";
secondOnes = "0";
setCountdownText();
timerStarted = false;
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
}
}.start();
timerStarted = true;
} else {
if (timerPaused == false) {
pauseTimer();
} else {
resumeTimer();
}
}
}
还有:
private void resumeTimer() {
timerPaused = false;
startTime = timeRemaining;
CountDownTimer countDownTimer = new CountDownTimer(startTime, interval) {
public void onTick(long millisUntilFinished) {
if (timerPaused == true) {
cancel();
} else {
timeRemaining = millisUntilFinished;
long remainder = millisUntilFinished;
minuteTens = df.format(millisUntilFinished / 600000);
remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
minuteOnes = df.format(remainder / 60000);
remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
secondTens = df.format(remainder / 10000);
remainder = remainder - (Integer.valueOf(secondTens) * 10000);
secondOnes = df.format(remainder / 1000);
setCountdownText();
}
}
public void onFinish() {
minuteTens = "0";
minuteOnes = "0";
secondTens = "0";
secondOnes = "0";
setCountdownText();
timerStarted = false;
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
}
}.start();
}
和重置方法:
private void resetScores() {
final TextView P1CountDownTimer = (TextView) findViewById(R.id.P1CountDownTimer);
final TextView P2CountDownTimer = (TextView) findViewById(R.id.P2CountDownTimer);
timerStarted = false;
timerPaused = false;
minuteTens = "0";
minuteOnes = "0";
secondTens = "0";
secondOnes = "0";
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
setCountdownText();
P1CountDownTimer.setVisibility(View.INVISIBLE);
P2CountDownTimer.setVisibility(View.INVISIBLE);
}
编辑:
这也是相关的。这是我首先实例化计时器,然后在重置后再次实例化计时器。从用户获取数据并将其转换为毫秒以传递到倒数计时器:
private void resetTimer() {
final TextView P1CountDownTimer = (TextView) findViewById(R.id.P1CountDownTimer);
final TextView P2CountDownTimer = (TextView) findViewById(R.id.P2CountDownTimer);
startTime = ((Tens * 60) + (Ones)) * 1000;
timeRemaining = startTime;
if (startTime > 0) {
long remainder = startTime;
minuteTens = df.format(startTime / 600000);
remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
minuteOnes = df.format(remainder / 60000);
remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
secondTens = df.format(remainder / 10000);
remainder = remainder - (Integer.valueOf(secondTens) * 10000);
secondOnes = df.format(remainder / 1000);
setCountdownText();
P1CountDownTimer.setVisibility(View.VISIBLE);
P2CountDownTimer.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:1)
让它工作的方法似乎是使你的计时器全局,然后在全局var上调用cancel 见https://stackoverflow.com/a/27730328/1410671 和https://stackoverflow.com/a/23630959/1410671
我假设这些方法都在你的Activity类中,在这种情况下你会在Activity类的顶部声明你的CountDownTimer
private CountDownTimer countDownTimer;
然后在其他方法中引用它,如
countDownTimer = new CountDownTimer(startTime, interval) {...}