2011-02-17 3 views
2

나는 카운터를 3에서 1로 표시하고 다른 작업으로 빠르게 전환해야하는 애플리케이션이 있습니다. TimerTask가이 작업에 적합합니까? 아무도 나에게 그것을 정확하게하는 방법의 예를 보여줄 수 있습니까?Android에서 카운터 구현

CountDownTimer Worked. 3 초 동안 타이머를 표시하는 코드입니다.

new CountDownTimer(4000, 1000) { 

      public void onTick(long millisUntilFinished) { 
       Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);  
       counter.startAnimation(myFadeOutAnimation); 
       counter.setText(Long.toString(millisUntilFinished/1000)); 
      } 

      public void onFinish() { 
       counter.setText("done!"); 
      } 
     }.start(); 
+0

@Sankar 가네을 : 당신을 얻을 수없는 이유는 무엇입니까? Elobrate하십시오. – Praveen

+0

당신은 타이머를 카운트 다운하고 10 초 동안 타이머를 설정할 수 있습니다. 타이머가 만료되면 새로운 액티비티가 발생합니다. –

+0

그래도 괜찮을 것 같습니다. [타이머 작업을 사용하여 UI를 업데이트하는 방법을 알아 보려면 여기를 클릭하십시오.] (http://developer.android.com/resources/articles/timed-ui-updates.html) – Reno

답변

11

에 대한 객체를 생성합니다.

3 초 카운트 예를 들어 당신의 카운터에 대해 원하는 경우

//new Counter that counts 3000 ms with a tick each 1000 ms 
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) { 
    public void onTick(long millisUntilFinished) { 
     //update the UI with the new count 
    } 

    public void onFinish() { 
     //start the activity 
    } 
}; 
//start the countDown 
myCountDown.start(); 
+0

작은 유형이 있습니다. CountDownTimer myCountDown = 새로운 CountdownTimer (3000, 1000) { 은 다음과 같아야합니다. CountDownTimer myCountDown = 새 CountDownTimer (3000, 1000) { 새 CountDownTimer의 자본금 : – AlAsiri

+0

간단하지만 확실! 감사 –

2
Use CountDown Timer as Shown below 

1 단계 :

Create Count Down Timer Class 


class MyCount extends CountDownTimer { 
     public MyCount(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 

     public void onFinish() { 
      dialog.dismiss(); 
      // Use Intent to Navigate from this activity to another 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      // TODO Auto-generated method stub 

     } 


     } 

2 단계는 : 내가 더 나은 CountDownTimer 사용하는 것이 클래스

MyCount counter = new MyCount(2000, 1000); // set your seconds 
    counter.start();