2017-02-27 5 views
0

나는 게임을 만들고 타이머를 사용자에게 표시하고 시간이 0 일 때 작업을 수행하는 onCreate 메서드를 사용합니다. 내 방식은 올바르게 작동하지만 작성한 이후로 작동합니다. onCreate 시간이 계속해서 카운트 다운되며 화면 등을 회전하면 타이머 초가 재설정됩니다. 이 문제가 발생하는 이유는 알지만 응용 프로그램이 일시 중지되었을 때 응용 프로그램을 중단하고 응용 프로그램이 계속 진행될 때 중단 할 수있는 방법을 찾지 못했습니다. 여기 내 코드는인터럽트에 countTimer() 저장 (안드로이드)

new CountDownTimer(15000, 1000) { 

      public void onTick(long millisUntilFinished) { 
       question.setText("seconds remaining: " + millisUntilFinished/1000); 
      } 

      public void onFinish() { 
       question.setText("done!"); 
       Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class); 
       startActivity(i2); 
      } 
     }.start(); 

내가 savedInstanceState에 milisUntilFinished과 savedInstanceState에 따라 타이머 시작을 저장해야하거나 더 쉬운 해결책은

편집이 : 그래서 내가 물어 봤어요 물건을 반영하려면 코드를 업데이트 . 이 방법 전화가 회전 할 때 타이머의 마지막 상태를 저장할 수 있지만 메뉴를 열 때 타이머는 여전히 멈추지 않는다 (응용 프로그램을 일시 중지합니다.)

if(savedInstanceState!=null){ 
      seconds = savedInstanceState.getInt("seconds"); 
      new CountDownTimer(seconds * 1000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        seconds = (int) millisUntilFinished; 
        question.setText("seconds remaining: " + millisUntilFinished/1000); 
       } 

       public void onFinish() { 
        question.setText("done!"); 
        Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class); 
        startActivity(i2); 
       } 
      }.start(); 
     }else { 
      new CountDownTimer(15000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        seconds = (int) millisUntilFinished; 
        question.setText("seconds remaining: " + millisUntilFinished/1000); 
       } 

       public void onFinish() { 
        question.setText("done!"); 
        Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class); 
        startActivity(i2); 
       } 
      }.start(); 
     } 

답변

0
CounDwonTimer mCountTimer; 
int seconds = 0; 
는 는

그럼 그냥 onPause에서 mCountTimer 취소하고 onResme '

protected void onResume(){ 
    super.onReusme(); 

    mCountTimer = new CountDownTimer(seconds*1000,1000){ 
     //......record the seconds 
    } 
} 

protected void onPause(){ 
    if(mCountTimer != null) 
     mCountTimer.cancel(); 
} 
`에서 시작

당신의 활동이 destory 일 경우 화면을 돌릴 때 onSaveInstanceState이 필요합니다. 그렇지 않으면이 방법이 필요 없습니다.

+0

onCreate에서 onResume으로 countTimer를 배치했을 때 작동했습니다. 나는 이것을 대답으로 받아 들인다. – Freshia

0

을 나는 다음과 같은 코드가

당신을 도울 것입니다 희망 는
private long startTime = 0L; 
private boolean threadStop = false; 
private Handler myHandler = new Handler(); 
private Runnable updateTimerMethod = new Runnable() { 
    public void run() { 
     if (isAdded()) { 
      timeInMillies = SystemClock.uptimeMillis() - startTime; 
      //finalTime = timeSwap + timeInMillies; 
      time.setText(miliSecondToMinSec(timeInMillies)); 

      if (!threadStop) 
       myHandler.postDelayed(this, 1000); 
     } 
    } 
}; 

@Override 
public void onStart() { 
    super.onStart(); 
    if (threadStop) { 
     threadStop = false; 
     startTime = SystemClock.uptimeMillis() - timeInMillies; 
     myHandler.postDelayed(updateTimerMethod, 0); 
    } 
} 

@Override 
public void onStop() { 
    threadStop = true; 
    super.onStop(); 
} 
+0

onCreate 내부에서 처리기를 사용해야합니까? isAdded는 사용되지 않습니다. 무엇을 확인합니까? – Freshia

+0

조각에 그 조각이 활동에 추가되는지 여부를 확인하기 위해 isAdded를 사용하는 이유가 있습니다. 그리고 네, 창작자를 창업하러 시작합니다. – sadat

+0

나는 대답을 upvote하지만 countTimer를 사용하기 때문에 다른 하나를 받아 들인다. 이것도 작동합니다 – Freshia