Android:countDownTimer.cancel()不起作用

时间:2016-08-04 11:33:24

标签: android countdowntimer

我已经完成了以下问题

CountDownTimer cancel() not Working

CountDownTimer.cancel() is not working in Android

这是代码

private CountDownTimer mCountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startCountDownTimer();
}

private class MyCountdownTimer extends CountDownTimer {

    public MyCountdownTimer(long timeInMiliSeconds, long interval) {
        super(timeInMiliSeconds, interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //DO SOMETHING HERE
    }

    @Override
    public void onFinish() {
        // DO SOMETHING HERE
    }
}

public startCountDownTimer(){
    mCountDownTimer = new MyCountdownTimer(timeInMiliSeconds, Constant.ONE_SECOND);
    countDownTimer.start();
}


public void restartTimer(){
    cancelCountDownTimer();

    // Restarting timer
    mCountDownTimer = new MyCountdownTimer(timeInMiliSeconds, Constant.ONE_SECOND);
    mCountDownTimer.start();
}


public void cancelCountDownTimer() {
    if (mCountDownTimer != null) {
        Log.d(TAG, "canceling timer");
        mCountDownTimer.cancel();
        mCountDownTimer = null;
    }
}

0 个答案:

没有答案