开发者

force screen output before a Thread.sleep

开发者 https://www.devze.com 2023-02-07 23:20 出处:网络
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).This means that if I do an output after

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). This means that if I do an output after the Thread.sleep it will overwrite the first output. If I so a fo开发者_开发知识库rceLayout on the View before calling the Thread.sleep it does no good. It appears that Android stores up all the output and displays them at the end of the program. If I run such code as straightforward Java apps on the PC it works as just fine as intended.


You should not do a sleep() on the UI thread, because that will block your entire UI (as you noticed).

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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号