2012-06-07 28 views
0

下面是代碼即時試圖調用內部onCreate()如何在等待線程完成時顯示進度對話框?

 _t = new TheThread(this); 
     pd = new ProgressDialog(this); 
     pd.setMessage("Trip Detection .."); 
     pd.show(); 
     _t.start(); 
     while(_t.isAlive()){ 
      //DO NOTHING..WIAITING TILL MAIN THREAD FISHIN 
     } 
     printToScreen(); 
     pd.dismiss();   

printToScreen()更新的列表視圖。列表視圖的內容使用tread _t更新。但是當我調用這個方法時,我沒有看到任何「等待」消息出現。電話凍結,因爲它曾經是之前(當我沒有在線程上運行內容)。有什麼建議麼 ?

+3

你必須使用的AsyncTask –

+1

一個不只是搪塞UI線程在等待後臺線程完成 – Vladimir

+0

沒有。如何使用它? – dinesh707

回答

1

下面摘錄會幫助你。

progressDialog.show(); 

    new Thread() 
     { 
     public void run() 
     { 
      //do async operations here 

      handler.sendEmptyMessage(0); 
     } 
     }.start(); 

    Handler handler = new Handler() 
      { 

       @Override 
       public void handleMessage(Message msg) 
       { 
        progressDialog.dismiss(); 
        super.handleMessage(msg); 
       } 

      }; 
0
class doback extends AsyncTask < URL, Integer, Long > { 

    protected Long doInBackground(URL...arg0) { 
     try { 

     } catch (Exception e) { 

     } 
     return null; 
    } 

    protected void onProgressUpdate(Integer...progress) { 

    } 

    protected void onPostExecute(Long result) { 
     try { 
      dialog.dismiss(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      dialog.dismiss(); 
     } 
    } 

} 
0

創建對話框如下

pd = ProgressDialog.show(this, "Loading..", "Please Wait..", true,false); 
new Thread(new Runnable() {     
      @Override 
      public void run() { 
               // your code here 
        } 
     }).start();    
1

使用的AsyncTask

private class DownloadingProgressTask extends 
     AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this); 

    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 
      downloadFile(b.getString("URL")); 
      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      Toast.makeText(ShowDescription.this, 
        "File successfully downloaded", Toast.LENGTH_LONG) 
        .show(); 
      imgDownload.setVisibility(8); 
     } else { 
      Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG) 
        .show(); 
     } 
    }