Android sleep()而不阻止UI [英] Android sleep() without blocking UI
问题描述
对于我的新Android应用程序,我需要一个函数,该函数会使我的应用程序超时3秒钟.我像这样尝试了函数"sleep()":
For my new Android application I need a function, that timeout my application for 3 Seconds. I tried the function "sleep()" like this:
seekBar1.setProgress(50); // Set something for my SeekBar
try{
Thread.sleep(3000); // Wait for 3 Seconds
} catch (Exception e){
System.out.println("Error: "+e); // Catch the exception
}
button.setEnabled(true); // Enable my button
这似乎可行,但是如果我正在运行该应用程序,它会像这样:等待3秒钟,设置进度并启用按钮.我要先设置进度,然后等待3秒钟,然后再启用按钮.
It seems to work, but if I was running the application it does it like this: Wait for 3 Seconds, set progress and enable button. I want first to set the progress and then wait for 3 seconds and only then to enable the button.
"sleep()"是供我使用的权利,还是我的应用程序按正确的顺序进行操作?
Is "sleep()" for the right for my use or what can I do else that my application does it in the right order?
推荐答案
您可以使用postDelayed()
这样的方法:
You can use postDelayed()
method like this:
handler=new Handler();
Runnable r=new Runnable() {
public void run() {
//what ever you do here will be done after 3 seconds delay.
}
};
handler.postDelayed(r, 3000);
这篇关于Android sleep()而不阻止UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!