我已经完成了以下问题
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;
}
}