简体   繁体   English

如何以编程方式导致Android延迟?

[英]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.

相关问题 如何在Android中以编程方式添加焦点? - How can I add focus programmatically in Android? 如何在Android中以编程方式删除图片? - How can I delete picture programmatically in Android? 从Eclipse插件中,我如何以编程方式使Eclipse编辑某个文件? - From an Eclipse plugin, how can I programmatically cause Eclipse to edit a certain file? 如何以编程方式强制停止使用Java的Android应用程序? - How can I programmatically force stop an Android app with Java? Android如何以编程方式获取图像按钮的ID? - Android how can i get the image button's id programmatically? 如何在Android中编辑以编程方式创建的按钮? - How can I edit programmatically created buttons in Android? 如何使用 java 以编程方式添加数字选择器( android 工作室) - How can I add a number picker programmatically with java ( android studio ) 如何在Android中的RelativeLayout中以编程方式在按钮旁边显示文本视图? - How can i display programmatically a textview next to a button in a RelativeLayout in Android? 如何在Android中以编程方式反编译dex文件? - How can I programmatically decompile dex files within Android? 如何转换 XML 以在 Android Java 中以编程方式查看 - How can I convert an XML to make a programmatically view in Android Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM