Android的UI冻结,而不是显示ProgressDialog [英] Android UI Freezes instead of displaying ProgressDialog
问题描述
我想数据的下载过程中显示进度对话框。
I am trying to display a progress dialog during downloading of data.
现在,这是我有:
final ProgressDialog pd = ProgressDialog.show(this, "Updating", "Loading...");
Thread td = new Thread(new Runnable(){
public void run(){
//time consuming download
pd.dismiss();
}
});
td.start(); // start the thread and use join to wait for it to return
td.join(); //wrapped in try catch not shown here
我不需要从下载进度更新,直到数据被下载的应用程序不应该做任何事情,这就是为什么我用加入 - 所以它会等待下载完成
I don't need progress updates from the download, and the app shouldn't do anything else until the data is downloaded, that's why I use join - so it'll wait for the download to finish.
我得到的结果是
- 键
- UI冻结
- 打开和关闭进度对话框闪烁,下载完成
我需要的是
- 键
- 进度对话框显示
- 进度对话框关闭,下载完成。
任何想法,为什么它没有出现马上,而是呈闪烁状态时,它是做?
Any idea why it's not showing right away, and instead flashing on and off when it is done?
推荐答案
在从未使用过加入
但是当我阅读文档,它说,另一个线程(UI线程此处),直到工作线程完成将被阻止。因此,这说明你的描述相当不错的表现。
In never used join
but when I read the documentation it says that the other thread (the UI thread here) will be blocked until the working thread is finished. So this explains the behavior you describe quite good.
使用一个 AsyncTask的代替。执行下载的 doInBackground
和处理在 onPostExecute
的结果,它会工作......
Use a AsyncTask instead. Do the download in doInBackground
and process the result in onPostExecute
and it'll work...
这篇关于Android的UI冻结,而不是显示ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!