2013-11-15 5 views
8

CountDownTimer를 다시 시작합니다. 나는 여기서 많은 질문을 읽었지 만 그 대답의 아무도 나를 도왔다. 나는 다음과 같은 코드android에서 CountDownTimer - 다시 시작하는 방법

if(Const.counter != null){ 
    Const.counter.cancel(); 
    Const.counter = null; 
} 


Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000); 
Const.counter.start(); 

를 사용하는 경우 나는 새로운 카운터를 시작하지만 이전도 작업을 계속합니다. 제발 해결해주세요.

+0

시도는 ... 유) ( – KOTIOS

답변

4

취소하고 다시 시작하면됩니다. 다음 예제는 작동합니다.

CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) { 

    @Override 
    public void onTick(long millisUntilFinished) {} 

    @Override 
    public void onFinish() { 
     isCounterRunning = false; 
    } 
}; 


boolean isCounterRunning = false; 

private void yourOperation() { 
    if(!isCounterRunning){ 
     isCounterRunning = true; 
     mCountDownTimer.start(); 
    } 
    else{ 
     mCountDownTimer.cancel(); // cancel 
     mCountDownTimer.start(); // then restart 
    } 

} 
+2

mCountDownTimer.cancel를 다시 esailt 수 cornometer 사용하는; // 카운터를 재설정하지 않습니다. –

2

나는 다른 트릭을 여기에서했습니다. 희망이 당신을 도울 것입니다.

if (myCountDownTimer != null) { 
      myCountDownTimer.cancel(); 
     } 
     myCountDownTimer = new MyCountDownTimer(10000, 500); 
     myCountDownTimer.start(); 
관련 문제