我想要一个持续时间为20秒和每5秒的计时器,它可以将布尔值从false更改为true,并且可以在其他时间内重置。 例如
Timer t = new Timer(20);
from seconds 1 - 4 : boolean false
second 5 : boolean true
答案 0 :(得分:1)
试试这个:
new CountDownTimer(20000, 5000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//your code here for setting the boolean
}
}.start();
答案 1 :(得分: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();