我在进程运行时使用 ProgressDialog 时遇到问题。我已经尝试了所有可能的不正确方法,并查看了许多网站,这些网站提供了我正在尝试做的事情的示例,但是,我仍然遇到了线程在 ProgressDialog 出现之前正在运行的问题。这是我最近的尝试:
new Thread(new Runnable() {
public void run() {
dialog = new ProgressDialog(EPD.this);
dialog.setMessage("Loading. Please Wait...");
dialog.show();
}
}).run();
getMostWanted();
除了尝试这种方式之外,我还尝试在 getMostWanted() 中创建一个新线程,但我仍然得到相同的结果。它会在 getMostWanted() 期间暂停约 4 或 5 秒,并且没有对话框。
在此先感谢您的帮助。
最佳答案
如果您在主线程上,您应该使用它来显示 ProgressDialog 并为 getMostWanted()
分出另一个线程。假设您希望 getMostWanted()
的结尾关闭对话框,您应该查看 AsyncTask :
private class GetMostWanted extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog;
public GetMostWanted() {
dialog = new ProgressDialog(EPD.this);
dialog.setMessage("Loading. Please Wait...");
}
protected void onPreExecute() {
dialog.show();
}
protected void doInBackground(Void... unused) {
getMostWanted();
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
这样你的处理是在 doInBackground()
的后台线程上执行的,然后在你完成后你可以在 onPostExecute()
的主线程上关闭对话框>.
现在您可以使用:
new GetMostWanted(dialog).execute();
关于具有线程问题的Android ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122714/