【问题标题】:How can I show a Dialog box from a thread other than the UI thread如何从 UI 线程以外的线程显示对话框
【发布时间】:2013-09-01 16:47:37
【问题描述】:

我的应用程序需要读取 GPS,因此在主线程上我启动了一个读取 GPS 的线程,但在显示“请稍候”对话框时遇到问题。我也使用处理程序绑定,但这也不起作用。从第二个线程控制“请稍候”对话框的最佳方法是什么?谢谢!

public void showWaitDialog() {

    prgDialog = new ProgressDialog(context);
    prgDialog.setTitle("Please wait.");
    prgDialog.setMessage("Please wait.");
    prgDialog.setCancelable(false);
    prgDialog.show();


}

【问题讨论】:

    标签: java android


    【解决方案1】:

    你可以:

    • 在您的 UI 线程中定义一个 Handler(例如在 Activity 中),然后将其传递给您的线程。现在从您调用handler.post(runnable) 的线程将要在UIThread 上执行的代码排入队列。

    • 在您的Activity 中定义一个BroadcastReceiver,然后从您的线程发送一个Intent,在Bundle 中包含必要的信息

    • 使用AsyncTask 和方法publishProgress()、onProgressUpdate()onPostExecute() 通知Activity 进度或任务何时完成

    • 使用runOnUiThread

    这取决于您的需求。对于短期运行的异步操作,AsyncTask 是一个不错的选择。

    【讨论】:

    • 您好,我尝试将处理程序传递给将Thread myThread = new myClass(); 更改为Thread myThread = new myClass(handler);,然后在run() 方法中接收它,将其更改为run(Handler handler); 但这没有用,什么是正确的怎么做?谢谢
    • 你调用了 handler.post(runnable) 吗?如果它仍然不起作用,您可以编辑您的帖子并使用代码更新它。
    • 是的,我做到了,它工作了,但我没有通过处理程序,我只是在 Activity 类上公开它,然后从 Thread 类中使用它,调用它 Activity.handler,但我的问题是如何将处理程序作为参数传递。谢谢!
    【解决方案2】:

    为什么不使用AsyncTask。您可以告诉onPreExecute() 上的任务显示“请稍候”对话框,然后onPostExecute(Result result) 您可以删除该对话框。这两种方法在 UI 线程上工作,而 doInBackground(Params... params) 在后台线程中发生。

    例子:

    private class GetGPSTask extends AsyncTask<null, null, null>{
    
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
                        showWaitDialog();  <-Show your dialog
        }
    
    
        @Override
        protected void doInBackground(null) {
    
                    //your code to get your GPS Data
        }
    
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
                        HideDialogbox(); <-Code to hide the dialog box
        }
    }
    

    如果需要,请记住更改模板类型。在它说 AsynTask 的地方,第一个值传递给doInBackground,第二个值是进度值,第三个值是从doInBackgroundonPostExecute 的返回值。

    【讨论】:

      【解决方案3】:

      正如其他答案正确建议的那样,您最好使用AsyncTask。以下是如何将其用于您的目的的示例:AsyncTask Android example。否则你也可以使用runOnUiThread 方法。从您的第二个线程内部对 UI 线程进行更改(例如:Dialogs 和 Toasts)。根据其documentation,它说:

      It runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

      例如;

      Your_Activity_Name.this.runOnUiThread(new Runnable() {
      
              @Override
              public void run() {
                  // your stuff to update the UI
                  showWaitDialog();
      
              }
          });
      

      display progressdialog in non-activity classLoading Dialog with runOnUiThread for update view on Android。 希望这可以帮助。

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 1970-01-01
        相关资源
        最近更新 更多