2017-11-15 25 views
0

특정 조건이 존재할 때 카운트 다운 타이머를 일시 중지하는 앱을 만들고 있습니다. 내 카운트 다운 타이머는 문자열 배열을 순환하여 텍스트 필드를 채 웁니다. 카운터는 0으로 내려갈 때마다 텍스트 필드를 재설정하고 다음 텍스트 세트로 채 웁니다. 심지어는 일시 중지 버튼이 있습니다. 그러나 텍스트 필드 중 하나의 특정 텍스트를 기반으로 카운트 다운 타이머를 프로그래밍 방식으로 일시 중지 할 수는 없습니다.조건에 따라 일시 중지를 프로그래밍 할 수 없습니다.

Heres는 내 타이머 코드 :

class MyTimer extends CountDownTimer { 

    //constructor for timer class 
    public MyTimer(long millisInFurture, long countDownInterval) { 
     super(millisInFurture, countDownInterval); 
    } 

    // this method called when timer is finished 
    @Override 
    public void onFinish() { 
     // reset all variables 
     clockText.setText(clockTime); 
     isRunning = false; 
     remainMilli = 0; 

     advanceLevel(); 
     goTimer(); 
    } 

    // this method is called for every iteration of time interval 
    @Override 
    public void onTick(long millisUntilFinished) { 
     remainMilli = millisUntilFinished; 

     //calculate minutes and seconds from milliseconds 
     String minute = "" + (millisUntilFinished/1000)/60; 
     String second = "" + (millisUntilFinished/1000)%60; 

     // apply style to minute and second 
     if((millisUntilFinished/1000)/60 < 10) { 
      minute = "0" + (millisUntilFinished/1000)/60; 
     } 
     if ((millisUntilFinished/1000)%60 < 10) { 
      second = "0" + (millisUntilFinished/1000)%60; 
     } 

     //update textview with remaining time 
     clockText.setText(minute + ":" + second); 
    } 

} 

여기 내 시작의 타이머에 대한 코드를 일시 정지 :

public boolean goTimer() { 
    breakCheck = blinds.getText().toString(); 
    Log.i(breakCheck, "level"); 

    if (isRunning) { 
     // cancel (Pause) timer when it is running 
     mTimer.cancel(); 
     mTimer = null; 
     isRunning = false; 
    } else { 
     if (remainMilli == 0) { 
      // start timer from initial time 
      mTimer = new MyTimer(blindTime, 1000); 

     } else { 
      //resume timer from where it is paused 
      mTimer = new MyTimer(remainMilli, 1000); 
     } 
     mTimer.start(); 
     isRunning = true; 
     } 

    return true; 

} 

... 그리고 여기의 각 루프 후 텍스트 필드를 변경하는 코드입니다 카운트 다운 타이머 :

그래서 카운트 다운이 끝날 때마다 텍스트 필드에 n ew 텍스트를 문자열 배열에서 가져옵니다. 내가 원하는 것은 블라인드 텍스트 필드가 "BREAK"로 표시 될 때 카운트 다운 타이머를 일시 중지하길 원합니다.

그래서, 난에 넣어 :

String blindCheck = blinds.getText().toString(); 

{mTimer.cancel (blindCheck는 "BREAK"를 ==) 경우(); }

...하지만 내가하는 일과 상관없이, 카운트 다운 타이머가 멈추지 않고 그대로 유지됩니다.

타이머의 Start/Pause 버튼은 아래쪽 탐색 막대에 있으며 잘 작동하므로 내 blindCheck == "BREAK"일 때 시계를 시작/일시 중지하는 방법을 실행 해 보았습니다. 어느 쪽이든 일하다.

나는 무엇을해야할지 잘 모르겠다. 어떤 도움을 주시면 감사하겠습니다. 감사합니다. 헨드 루

답변

0

이 용도로 CountDownTimer를 사용하지 마십시오. 대신이 핸들러를 사용해야합니다.

final Handler handler = new Handler(); 
    final Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 

      // done your functionality here 


      // if you want to continue, or pause is not active 
      handler.postDelayed(this, 1*1000); 

      // else you will in pause state 

     } 
    }; 


    // to start the hanlder 
    handler.postDelayed(runnable, 1*1000); 

    // to stop or pause the handler 
    handler.removeCallbacks(runnable); 
+0

그래서 지금 가지고있는 모든 것을 제거해야합니까? 왜냐하면 핸들러를 사용하는 것이 더 좋은 방법 일 뿐이므로 내가 가진 것과 함께 할 수 없기 때문입니까? 내가 지금 가지고있는 모든 것이 잘 작동하기 때문에 ... 특정 이벤트 동안 멈추지 않는 것만 빼면. 고마워, 헨도. –

+0

나는 배수로를 드릴께요 –

+0

글쎄,이 방법은 잘 작동하지만 텍스트 필드가 BREAK 때 자동으로 멈추게 할 수는 없습니다. –