[英]How can I programmatically cause a delay in Android?
I tried to use Thread.sleep()
but it didn't work. 我尝试使用
Thread.sleep()
但是没有用。 When I use it, the app stops responding. 当我使用它时,该应用程序停止响应。
I need to put some delays in my code like this: 我需要在代码中添加一些延迟,例如:
public void inicioJogo(){
for (int jogada = 1; jogada <= 50; jogada++) {
for (int contador = 0; contador < jogada; contador++){
// HERE - Wait 1 sec before go to next line.
btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
// HERE - Wait 1 sec before go to next line.
btPedra[sequencia[contador]].setBackgroundResource(imagensNormal[sequencia[contador]]);
// Now continue looping.
}
}
}
I tried to use Handler, like this: 我尝试使用Handler,如下所示:
private Handler handler = new Handler();
for (int jogada = 1; jogada <= 50; jogada++) {
for (int contador = 0; contador < jogada; contador++){
handler.postDelayed(new Runnable () {
@Override
public void run() {
btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
}
}, 1000);
}
}
But when I use it, the looping continue before waiting 1 sec. 但是,当我使用它时,循环将继续,然后等待1秒。 I need a delay that can stop the looping for 1 sec, go to the next line, and after that continue looping.
我需要一个可以使循环停止1秒钟的延迟,转到下一行,然后继续循环。
You can use ScheduledExecutorService
like 您可以使用
ScheduledExecutorService
类的
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(NO_OF_THREADS);
ScheduledFuture scheduledFuture =
scheduledExecutorService.schedule(task, NO_OF_SECONDS_TO_WAIT, TimeUnit.SECONDS);
and now the "task" executes after a delay of NO_OF_SECONDS_TO_WAIT 现在,在延迟NO_OF_SECONDS_TO_WAIT之后执行“任务”
The reason why it doesn't respond is because you call sleep on the current thread. 它没有响应的原因是因为您在当前线程上调用了sleep。 And the current thread you're dealing with is the UI thread.
您正在处理的当前线程是UI线程。 So basically you try to change the background and then sleep the thread preventing it to actually be changed until the sleeps are done.
因此,基本上,您尝试更改背景,然后使线程休眠,以防止实际更改线程,直到完成休眠为止。
You need to run your loop in another thread and sleep there so it doesn't affect the main thread: 您需要在另一个线程中运行循环并在那里休眠,这样才不会影响主线程:
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
for (int jogada = 1; jogada <= 50; jogada++) {
for (int contador = 0; contador < jogada; contador++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
}
});
}
}
}
}).start();
Note that the background change is done via the handler you create before starting the new thread. 请注意, 在启动新线程之前,通过您创建的处理程序来完成后台更改。 That handler is created on the current thread, which is the UI thread, and every message posted to it will run on that thread.
该处理程序是在当前线程(即UI线程)上创建的,发布到它的每个消息都将在该线程上运行。
So basically you loop and wait on another thread but do the background changing on the UI thread. 因此,基本上,您循环并在另一个线程上等待,但在UI线程上进行了后台更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.