设置布尔与定时器android

设置布尔与定时器android

问题描述:

我想有一个计时器,如20秒,并在每隔5秒,这可能会将布尔值从false更改为true,它可以在其他秒复位。 例如设置布尔与定时器android

Timer t = new Timer(20); 
from seconds 1 - 4 : boolean false 
second 5 : boolean true 
+0

是解决真正的工作?我认为你需要修改它。 –

+0

@AuuragSingh是啊我可以在onTick函数中设置布尔值,并将其重置为false,以调用真实条件的函数 –

试试这个:

new CountDownTimer(20000, 5000) { 

    public void onTick(long millisUntilFinished) { 

    } 

    public void onFinish() { 
     //your code here for setting the boolean 
    } 
    }.start(); 
+0

我认为您的评论应该位于设置布尔值的顶部框中。 –

new CountDownTimer(20000, 1000) { 
     public void onTick(long millisUntilFinished) { 
     long seconds = millisUntilFinished/1000; 
     Log.d(TAG, "onTick:: seconds="+seconds); 
     if ((seconds % 5) == 0) { 
      Log.d(TAG, "onTick:: 5 seconds lap"); 
      //set your boolean variable to true 
     }else{ 
      //set your boolean variable to false 
     } 
     } 

     public void onFinish() {} 
    }.start(); 
+0

因为你使用了1秒的冷却时间,我认为你也是对的 –