我正在编写一个Activity,它通过HTTP从Web中获取一些XML,然后将其解析为DOM。然后它从DOM中提取一些所需的数据。
您可以想象,这需要一些时间,因此我将该代码放入其自己的线程中,然后尝试设置ProgressDialog以在用户等待完成时显示。
问题是ProgressDialog根本不显示。如果我删除了对dismiss()的调用,那么它会在工作完成后显示,显然,只是坐在那里......
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = ProgressDialog.show(this, "", "Loading...", true);
Thread downloadThread = new Thread(new DownloadVerseThread());
downloadThread.start();
setContentView(R.layout.main);
TextView verseView = (TextView) findViewById(R.id.displayFighterVerse);
TextView dateView = (TextView) findViewById(R.id.displayDateRange);
TextView referenceView = (TextView) findViewById(R.id.displayReference);
try {
downloadThread.join();
} catch (InterruptedException e) {
Log.e(TAG, "Attempted to set the UI values before we were ready.");
return;
}
Log.d(TAG, "Date: " + date);
Log.d(TAG, "Reference: " + reference);
Log.d(TAG, "Scripture: " + scripture);
progressDialog.dismiss();
dateView.setText(date);
referenceView.setText(reference);
verseView.setText(scripture);
}
private class DownloadVerseThread implements Runnable {
@Override
public void run() {
try {
FighterVerseDownloader downloader = new FighterVerseDownloader();
date = downloader.getDateRange();
reference = downloader.getReference();
scripture = downloader.getScripture();
} catch (FighterVersesException e) {
Log.e("FighterVerses", "Failed to get the Fighter Verse", e);
}
}
}
FighterVersesDownloader的构造函数是完成HTTP和DOM方面的所有工作的地方。
关于我缺少的任何想法?我已经在使用AsyncTask看到了一些相似的线程,但是我想直接使用好的老式Threads并且建议的解决方案不适用。
答案 0 :(得分:3)
我建议使用AsyncTask来处理这类事情。
答案 1 :(得分:3)
我认为问题可能与你如何进行线程有关。当你调用downloadThread.join()时,你基本上就是拿着那些昂贵的代码并在方法中间把它打好,在这个过程中阻塞了UI线程。
当线程完成时,您需要使用回调方法来处理UI更改。您的代码应如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = ProgressDialog.show(this, "", "Loading...", true);
Thread downloadThread = new Thread(new DownloadVerseThread());
Handler downloadHandler = new Handler();
Runnable downloadCallback = new Runnable (){
public void run(){
setContentView(R.layout.main);
TextView verseView = (TextView) findViewById(R.id.displayFighterVerse);
TextView dateView = (TextView) findViewById(R.id.displayDateRange);
TextView referenceView = (TextView) findViewById(R.id.displayReference);
Log.d(TAG, "Date: " + date);
Log.d(TAG, "Reference: " + reference);
Log.d(TAG, "Scripture: " + scripture);
progressDialog.dismiss();
dateView.setText(date);
referenceView.setText(reference);
verseView.setText(scripture);
}
};
downloadThread.start();
}
private class DownloadVerseThread implements Runnable {
@Override
public void run() {
try {
FighterVerseDownloader downloader = new FighterVerseDownloader();
date = downloader.getDateRange();
reference = downloader.getReference();
scripture = downloader.getScripture();
downloadHandler.post(downloadCallback);
} catch (FighterVersesException e) {
Log.e("FighterVerses", "Failed to get the Fighter Verse", e);
}
}
}
这会将后续线程中的下载保留到完成之前,而不是像以前那样立即将其分流回UI线程。