我正在尝试使用这个计时器,就像在这个问题上建议的那样:
https://stackoverflow.com/questions/13806545/how-to-extend-countdown-timer-with-pause,
但没有按预期工作。暂停/恢复工作正常,但如果我取消并重新创建计时器,则计数从上次暂停时间开始。我需要从初始值开始。例如,计数器的初始值为 3 分钟。如果我在 2 分钟时暂停它,那么当我尝试再次创建它时,它会从 2 分钟开始。有什么帮助吗?
public class MainActivity extends ButtonMethods implements OnClickListener {
private CountDownTimerWithPause timerPausable = null;
int milis = 180000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerPausable = new CountDownTimerWithPause(milis, 1000, true)
{
@Override
public void onTick(long millisUntilFinished)
{
timer.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish()
{
timer.setText("180");
DisableRandomButtons();
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bNewWord:
if(clicked == false){
clicked = true;
timerPausable.create();
}
else{
clicked = false;
timerPausable.cancel();
timerPausable=null;
milis=180000;
timerPausable.create();
}
break;
case R.id.imageView2:
tapsound.start();
if(isclicked == false){
isclicked = true;
timerPausable.pause();
}
else{
isclicked = false;
timerPausable.resume();
}
break;