暂停和恢复 android 倒数计时器
Pause and resume android countdown timer
我有 android 自动启动的倒数计时器。我需要暂停并恢复它。我见过其他解决方案,但它们没有用。我的代码是:
class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
timerTimedOut = false;
}
@Override
public void onFinish() {
if(timerTimedOut){
doSTH();
} else {
doSTHElse();
}
this.start();
}
@Override
public void onTick(long millisUntilFinished)
{
timerShow.setText(Long.toString(millisUntilFinished / 1000));
}
public void stop(){
timerTimedOut = true;
this.cancel();
onFinish();
}
}
我应该如何暂停和恢复它?
我一直在处理这个问题,在尝试了很多事情之后,我找到了这个解决方案。在这里,您可以找到 Android CountDownTimer class 的绝佳替代品:
https://gist.github.com/bverc/1492672
您只需创建一个名为 CountDownTimer2 的新 class,然后粘贴该代码。然后,用它代替普通的 CountDownTimer class,例如:
CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//Whatever you want to do onTick
}
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
然后你只需要通过
cdt.pause();
或
cdt.resume();
希望对您有所帮助。
我有 android 自动启动的倒数计时器。我需要暂停并恢复它。我见过其他解决方案,但它们没有用。我的代码是:
class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
timerTimedOut = false;
}
@Override
public void onFinish() {
if(timerTimedOut){
doSTH();
} else {
doSTHElse();
}
this.start();
}
@Override
public void onTick(long millisUntilFinished)
{
timerShow.setText(Long.toString(millisUntilFinished / 1000));
}
public void stop(){
timerTimedOut = true;
this.cancel();
onFinish();
}
}
我应该如何暂停和恢复它?
我一直在处理这个问题,在尝试了很多事情之后,我找到了这个解决方案。在这里,您可以找到 Android CountDownTimer class 的绝佳替代品: https://gist.github.com/bverc/1492672
您只需创建一个名为 CountDownTimer2 的新 class,然后粘贴该代码。然后,用它代替普通的 CountDownTimer class,例如:
CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//Whatever you want to do onTick
}
@Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
然后你只需要通过
cdt.pause();
或
cdt.resume();
希望对您有所帮助。