Java 使倒计时立即执行,然后每隔x秒执行一次
我有一个倒计时装置,当点击一个按钮时,它每10秒执行一段特定的代码。但它只在点击按钮10秒后执行代码。如何让它立即执行,然后每隔一秒执行一次Java 使倒计时立即执行,然后每隔x秒执行一次,java,android,countdowntimer,Java,Android,Countdowntimer,我有一个倒计时装置,当点击一个按钮时,它每10秒执行一段特定的代码。但它只在点击按钮10秒后执行代码。如何让它立即执行,然后每隔一秒执行一次 CountDownTimer countDown; public void onButtonClick (View v) throws IOException, InterruptedException { countDown = new CountDownTimer(10000,10000) { @Overri
CountDownTimer countDown;
public void onButtonClick (View v) throws IOException, InterruptedException {
countDown = new CountDownTimer(10000,10000)
{
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
start();
//codes
}.start();
}
}
倒计时(长毫秒未来,倒计时间隔)
“millisInfutre”表示从start()开始到调用onFinish()的毫秒数
将“毫伏”设置得更大。我认为没有必要在倒计时的时候打第一个电话。直接打电话就行了
someYourTask(?);
new CountDownTimer(Long.MAX_VALUE, 10000) {
@Override
public void onTick(long l) {
someYourTask(?);
}
@Override
public void onFinish() {
Log.d("SOME_TAG", "FINISH");
}
}.start();
? someYourTask(?) {}
如果你想每10秒执行一次代码,我建议你做些别的事情 执行startRepeatingTask();单击按钮时
private int interval = 10000; //every 10 seconds
private Handler handler;
Runnable codeExecuter = new Runnable() {
@Override
public void run() {
try {
//run your code
} finally {
handler.postDelayed(codeExecuter, interval);
}
}
};
void startRepeatingTask() {
codeExecuter.run();
}
void stopRepeatingTask() {
handler.removeCallbacks(codeExecuter);
}