Android的ProgressDialog与线程的问题 [英] Android ProgressDialog with threading problem

查看:136
本文介绍了Android的ProgressDialog与线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到使用ProgressDialog过程正在运行的问题。我想尽不正确可能的方式,并看着它提供了什么,我想不过做例子,我仍然运行到该线程运行的问题之前,ProgressDialog不断出现无数的网站。这里是我的最新尝试这个:

I am running into a problem using the ProgressDialog while a process is running. I have tried every incorrect way possible and have looked at numerous websites which offered examples of what I am trying to do however, I am still running into the problem that the thread is running before the ProgressDialog ever comes up. Here is my latest attempt at this:

new Thread(new Runnable() {
     public void run() {
        dialog = new ProgressDialog(EPD.this);
        dialog.setMessage("Loading. Please Wait...");
        dialog.show();         
                    }
 }).run();
 getMostWanted();                       

在除了尝试这种方式,我也尝试一个新的线程中getMostWanted(),但是我仍然有同样的结果。它会暂停〜4或5秒,同时getMostWanted(),没有对话框。

In addition to trying this way, I have also attempted to a new Thread in getMostWanted() however I am still having the same result. It pauses for ~4 or 5 seconds while getMostWanted() and no dialog box.

在此先感谢您的帮助。

推荐答案

如果你是在主线程,你应该用它来显示ProgressDialog和分拆另一个线程的 getMostWanted()。假设你想要的结局getMostWanted()来关闭对话框,你应该看看的 AsyncTask的

If you're on the main thread, you should use that to display the ProgressDialog and spin off another thread for getMostWanted(). Assuming you want the ending of getMostWanted() to dismiss the dialog, you should look at 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在后台线程进行(),然后就大功告成后,<$ C,你可以关闭该对话框的主线程$ C> onPostExecute()。

That way your processing is performed on a background thread in doInBackground() and then after you're done you can dismiss the dialog on the main thread in onPostExecute().

现在你可以使用:

新GetMostWanted(对话).execute();

这篇关于Android的ProgressDialog与线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆