2013-07-31 68 views
2

我在this中使用了建议,但我还有同样的问题。 感谢我的朋友提出使用AsyncTask的建议,但它对我无效。 什么? 这是我的代码: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; 
    } 

任何人都可以帮助我吗?

+0

关闭进度应该在onPostExecute。 –

+0

没有我的过程是关闭它之前 –

+1

@ArmaanStranger为什么会这样。只要doInBackground完成其执行,就会自动调用onPostExecute。 – ObieMD5

回答

3

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

+0

谢谢!但我需要等待和睡眠程序。我的过程很复杂,我不会在onPostExecute中设置下一个状态。我正在寻求解决这个问题。 –

1

它应该是这样的:

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(); 
      } 
       }