2017-04-04 61 views
0

我想有一個計時器,如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,以調用真實條件的函數 –

回答

1

試試這個:

new CountDownTimer(20000, 5000) { 

    public void onTick(long millisUntilFinished) { 

    } 

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

我認爲您的評論應該位於設置布爾值的頂部框中。 –

1
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秒的冷卻時間,我認爲你也是對的 –