2012-06-07 46 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); 
相关问题