简体   繁体   English

在Thread.sleep之前强制屏幕输出

[英]force screen output before a Thread.sleep

If I try to do a screen output (Toast or setContentView) before a Thread.sleep it will not be displayed until after the sleep (or all the sleeps in the thread). 如果我尝试在Thread.sleep之前执行屏幕输出(Toast或setContentView),则直到睡眠(或线程中的所有睡眠)之后才会显示该屏幕。 This means that if I do an output after the Thread.sleep it will overwrite the first output. 这意味着,如果我在Thread.sleep之后执行输出,它将覆盖第一个输出。 If I so a forceLayout on the View before calling the Thread.sleep it does no good. 如果我在调用Thread.sleep之前在View上使用了forceLayout,那就不好了。 It appears that Android stores up all the output and displays them at the end of the program. 看来Android会存储所有输出,并在程序末尾显示它们。 If I run such code as straightforward Java apps on the PC it works as just fine as intended. 如果我在PC上将这些代码作为简单的Java应用程序运行,则可以按预期运行。

You should not do a sleep() on the UI thread, because that will block your entire UI (as you noticed). 您不应该在UI线程上执行sleep() ,因为这会阻塞整个UI(如您所注意到的)。

If you want something to happen after a certain amount of time, you can post a delayed message by creating a Handler and using its postDelayed() method. 如果您希望在一定时间后发生某些事情,可以通过创建Handler并使用其postDelayed()方法来发布延迟的消息。

Hand the job to a Handler. 将作业交给处理程序。 I hope you aren't doing Thread.sleep on the UI thread, because that will always cause the screen to freeze (and possibly your program to be terminated as unresponsive). 我希望您不要在UI线程上执行Thread.sleep,因为这将始终导致屏幕冻结(并且可能由于无响应而终止程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM