【问题标题】:AsyncTask not show progress dialog immediateAsyncTask 不立即显示进度对话框
【发布时间】:2013-07-31 12:54:53
【问题描述】:

我在this 中使用了建议,但我还有同样的问题。 感谢我的朋友对使用 AsyncTask 的建议,但它对我不起作用。 什么? 这是我的代码:

DBAsyncTask dba = new DBAsyncTask(this, xmlCommand);
    dba.inProcess = true;
    dba.execute("");
    while (dba.inProcess) {
        try {
            Thread.sleep(200);
            println("wwwwait");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public class DBAsyncTask extends AsyncTask<String, Void, String> {
    public boolean inProcess;

    public DBAsyncTask(ExirCommandRunner commandRunner,XmlNode xmlCommand) {
        this.commandRunner = commandRunner;
        this.xmlCommand = xmlCommand;

    }

    @Override
    protected void onPostExecute(String result) {
        ExirDebugger.println("onPostExecute");
    }

    @Override
    protected void onPreExecute() {
        showProgress();
    }

    @Override
    protected String doInBackground(String... params) {
    ///my process here 
        closeProgress();
        return null;
    }

谁能帮帮我?

【问题讨论】:

  • 关闭进度应该在onPostExecute。
  • 没有我的进程在关闭之前
  • @ArmaanStranger 为什么会这样。一旦 doInBackground 完成执行,就会自动调用 onPostExecute。
  • @ObieMD5 是的,它会被自动调用。所以关闭对话框应该在那个方法中。
  • @alishekari 你应该在 PostExecute 方法中关闭对话框。

标签: android android-asynctask progressdialog


【解决方案1】:

Thread.sleep() 阻塞 UI 线程,因此无法运行进度对话框。删除它和inProcess 轮询机制。此外,还有很多 references 使用带有进度对话框的异步任务。

【讨论】:

  • 谢谢!但我需要等待和睡眠程序。我的过程很复杂,我没有在 onPostExecute 中设置下一个状态。我正在寻求解决这个问题。
【解决方案2】:

我在这里解释了AsyncTask中ProgressDialog的详细用法和解决方案: https://stackoverflow.com/a/17527979/1943671

【讨论】:

    【解决方案3】:

    应该是这样的:

    protected void onPreExecute() {
                super.onPreExecute();
                 pDialog = new ProgressDialog(activityContext);
                 pDialog.setCancelable(false);
                 pDialog.setMessage("Please Wait..");
                 pDialog.show();
            }
    

    protected void onPostExecute(String file_url) 
            {
                if(pDialog.isShowing())
                {
                    pDialog.cancel();
                }
                    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多