【问题标题】:Dismiss a dialog when a thread is done线程完成后关闭对话框
【发布时间】:2011-10-28 14:47:55
【问题描述】:

我正在 android 中创建一个线程来进行耗时的操作。我希望主屏幕显示一个进度对话框,其中包含一条消息,通知操作正在进行中,但我希望该对话框在线程完成后关闭。我试过加入,但它锁定了线程并且不显示对话框。我尝试使用:

dialog.show();
mythread.start();
dialog.dismiss(); 

但随后对话框不显示。如何进行该序列但等待线程结束而不锁定主线程?

据我所知:

public class syncDataElcanPos extends AsyncTask<String, Integer, Void> {
    ProgressDialog pDialog;
    Context cont;
    public syncDataElcanPos(Context ctx) {
        cont=ctx;
    }

    protected void onPreExecute() {
    pDialog = ProgressDialog.show(cont,cont.getString(R.string.sync), cont.getString(R.string.sync_complete), true);
}

protected Void doInBackground(String... parts) {        
   // blablabla...
   return null;
}

protected void onProgressUpdate(Integer... item) {
    pDialog.setProgress(item[0]); // just for possible bar in a future.
}

protected void onPostExecute(Void unused) {
    pDialog.dismiss();
}

但是当我尝试执行它时,它给了我一个异常:“无法添加窗口”。

【问题讨论】:

    标签: android multithreading


    【解决方案1】:

    线程完成后,使用 runOnUIThread 方法关闭对话框。

    runOnUiThread(new Runnable() {
    public void run() {
            dialog.dismiss();
        }
    });
    

    【讨论】:

      【解决方案2】:

      要做到这一点,有两种方法,(我更喜欢第一种方法:AsyncTask):

      首先:你显示你的 alertDialog ,然后在方法 run() 你应该这样做

      @override
      public void run(){
      //the code of your method run 
      //....
      .
      .
      .
      //at the end of your method run() , dismiss the dialog
      YourActivity.this.runOnUiThread(new Runnable() {
      public void run() {
              dialog.dismiss();
          }
      });
      
      }
      

      第二:使用这样的 AsyncTask:

      class AddTask extends AsyncTask<Void, Item, Void> {
      
          protected void onPreExecute() {
      //create and display your alert here 
          pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Downloading data ...", true);
      }
      
      protected Void doInBackground(Void... unused) {
      
          // here is the thread's work ( what is on your method run()
          items = parser.getItems();
      
          for (Item it : items) {
              publishProgress(it);
          }
          return(null);
      }
      
      protected void onProgressUpdate(Item... item) {
          adapter.add(item[0]);
      }
      
      protected void onPostExecute(Void unused) {
          //dismiss the alert here where the thread has finished his work
          pDialog.dismiss();
      }
        }
      

      【讨论】:

        【解决方案3】:

        AsyncTask 在执行后你可以调用dismiss

        这是来自其他线程的示例

        class AddTask extends AsyncTask<Void, Item, Void> {
        
            protected void onPreExecute() {
                pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
            }
        
            protected Void doInBackground(Void... unused) {
                items = parser.getItems();
        
                for (Item it : items) {
                    publishProgress(it);
                }
                return(null);
            }
        
            protected void onProgressUpdate(Item... item) {
                adapter.add(item[0]);
            }
        
            protected void onPostExecute(Void unused) {
                pDialog.dismiss();
            }
          }
        

        【讨论】:

        • 感谢大家的帮助,将尝试 AsyncTask 选项,看看效果如何...
        • 如何在进度对话框中添加上下文? MyActivity.this 对我不起作用,说它无法使用这些参数创建,它采用的是活动本身而不是上下文。
        • 您是否将 AddTask 类声明为您的 Activity 的 InnerClass?如果是,那么它应该可以工作,如果不是,那么你应该将上下文传递给你的类 AddTask
        • 是的,我已通过任务,但我现在遇到了异常,有什么想法吗?代码在主要问题部分。
        • 在您的 Activity 中,在 onCreate 方法中,您应该像这样实例化您的类 syncDataElcanPos:syncDataElcanPos instance = new syncDataElcanPos(this);
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多