【问题标题】:Show a Dialog inside a thread that it isn't GUI thread在不是 GUI 线程的线程内显示一个对话框
【发布时间】:2012-04-12 16:18:46
【问题描述】:

我正在开发 Android 3.1 及更高版本的应用程序。

我添加了一个线程来使用 Spring Framework 发出 REST 请求。这是我的代码:

public class FormsListActivity extends ListActivity
{
    private List<Form> forms;
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      progressDialog = ProgressDialog.show(this, "Please Wait.", "Loading forms...");
      getUpdates.start();
    }

    private Thread getUpdates = new Thread ()
    {
        public void run() 
        {
            try
            {
                forms = FormSpringController.LoadAll();
                progressDialog.dismiss();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                //display = e.getLocalizedMessage();
            }

            runOnUiThread(new Runnable()
            {
                public void run()
                {
                    setListAdapter(new FormAdapter(FormsListActivity.this, R.layout.form_list_item, forms));

                    ListView lv = getListView();
                    lv.setTextFilterEnabled(true);
                }
            });
        }
    };
}

我想在 catch 块中显示一个对话框:

catch (Exception e)
{
    e.printStackTrace();
    //display = e.getLocalizedMessage();
}

我想设置display 变量是否有错误,然后在runOnUiThread(new Runnable() 中检查它。如果 display 有值,则显示一个对话框。

你怎么看?有没有更好的办法?

【问题讨论】:

  • 您无法从普通线程更新 UI 或显示 AlertDialog。您必须使用 UI 线程来执行此操作

标签: android multithreading dialog ui-thread


【解决方案1】:

看看这个。把你线程的run方法的所有代码放在handleMessage(Message message)方法中

private Handler handler = new Handler(){
        public void handleMessage(Message message){
             try
             {
                 forms = FormSpringController.LoadAll();
                 progressDialog.dismiss();
                 setListAdapter(new FormAdapter(FormsListActivity.this, R.layout.form_list_item, forms));

                 ListView lv = getListView();
                 lv.setTextFilterEnabled(true);
             }
             catch (Exception e)
             {
                 e.printStackTrace();
                 //display = e.getLocalizedMessage();
             }

        }
    }

并在你的线程的运行方法中调用它

handler .sendEmptyMessage(0);

【讨论】:

    【解决方案2】:
    runOnUiThread(new Runnable(){
    public void run(){
    ////code for alert dialog
    }
    });
    

    不推荐,但您可以使用它。最好使用另一个处理所有 UI 任务的线程。

    【讨论】:

    • 感谢您的回答。你能说我在哪里可以找到一些关于“处理所有 UI 任务的线程”的示例代码吗?谢谢
    • 因为FormSpringController.LoadAll() 是一个 REST GET 请求(我认为它是一个同步调用)。你可以在这里看到FormSpringController.LoadAll()源代码stackoverflow.com/questions/10073290/…
    • 如果那是更好的 uo 使用异步任务
    • 谢谢,但是,如何在 catch 块中显示一个对话框?我想我不能。谢谢。
    • 在你的 catch 块中尝试上面提到的解决方案
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多