2012-06-07 49 views
3

我已經使用倒數計時器這樣安卓:完成(CountDownTimer的)被調用,即使取消()被調用

新CountDownTimer(15000,15){

   public void onTick(long millisUntilFinished) { 

       long seconds=millisUntilFinished/1000; 
       long min=millisUntilFinished%100; 

       timeleft=(int) (seconds*1000+min); 
       if(millisUntilFinished>=10000) 
       { 
        changeText.setTextColor(Color.GREEN); 
       } 
       else if(millisUntilFinished>=5000) 
       { 
        changeText.setTextColor(Color.MAGENTA); 
       } 
       else 
       { 
        changeText.setTextColor(Color.RED); 

       } 
       changeText.setText(String.format("%02d", seconds)+ "."+String.format("%02d", min)+" sec"); 

      } 

      public void onFinish() { 

       timeleft=0; 
       missed++; 
        nametext.setTextColor(Color.RED); 
       nametext.setText("Time Up!"); 
         bottombutton.setVisibility(View.INVISIBLE); 
        globalflag=13; 
       changeText.setTextColor(Color.RED); 
       changeText.setText("0.00 Sec"); 
        Handler myHandler = new Handler(); 
        myHandler.postDelayed(mMyRunnablecif, 3000); 



      } 
      }.start(); 

上的按鈕點擊我已經調用了cancel(),但它停止計數一段時間,然後調用onFinish()。我不需要在調用cancel()之後調用onFinish()是否有任何解決方案。任何幫助將不勝感激。

回答

3

onClick裏面設置一個布爾值(例如buttonPressed)爲true。

在你onFinish檢查這個布爾:

if (buttonPressed == true) 
{ 
    //do nothing 
} 
else 
{ 
    //run code 
} 
+0

不onFinish( )回顧它所屬的活力?或者調用oncreate() – Moyeen

+0

onFinish()關於倒數計時器未連接到活動。這只是倒數計時器完成時調用的方法。 – Tony

2

你可以使用Timer來代替,而做這樣的事情:

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     // do your updates here 
     mUpdateTimeHandler.postDelayed(this, 1000); 
    } 
}; 

Handler mUpdateTimeHandler = new Handler(); 
mUpdateTimeHandler.postDelayed(mUpdateTimeTask, 100); 

當取消任務:

mUpdateTimeHandler.removeCallbacks(mUpdateTimeTask); 
相關問題