2011-04-29 46 views

回答

1

您可以使用ThreadAsyncTask,或Service到在後臺加載數據,並與Handler實現控制你ProgressDialog

The example in this post顯示如何使用線程進行登錄請求,同時顯示進度對話框。

使用AsyncTask是一個極大的方便,更清晰的:

private static final int WAIT = 11; 
private final class MyTask extends AsyncTask<Void, Void, Void> 
{ 
    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     // Show up the dialog with id=WAIT [11] 
     showDialog(WAIT); 
     // other actions that must be performed in the UI thread 
     // before the background works starts 
    } 

    @Override 
    protected Void doInBackground(Void... params) 
    { 
     // perform the background work 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
     super.onPostExecute(result); 
     // Remove the dialog with id=WAIT [11] 
     removeDialog(WAIT); 
     // other actions that must be performed in the UI thread 
     // after the background works finished 
    } 
} 

[...] 

final MyTask task = new MyTask(); 
task.execute(null); 

由於AsyncTask是一個泛型類型,你可以指定參數類型爲您的喜好,所以它是從UI線程傳輸數據非常方便一個後臺線程和後面。

你對話的部分是隻有活動中的幾行:

private ProgressDialog dialog; 

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    switch (id) 
    { 
     case WAIT: 
     { 
      dialog = new ProgressDialog(this); 
      dialog.setMessage("Loading..."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(true); 
      return dialog; 
     } 
    } 
    return null; 
} 
0

將您的網絡進程代碼移動到Thread並獲得ProgressDialog。當您完成網絡過程時,請撥打.start();,然後撥打ProgressDialog.show();,啓動您的網絡進程,通過HandlerThread.run()停止ProgressDialog

0

你可以嘗試在烏拉圭回合線程進步dialoge這個代碼

ProgressDialoge PD = ProgressDialog.show(這一點,「請稍候...「,」檢索數據「,true,false);

1

此任務通常通過帶有進度對話框的AsyncTask來解決。看到這個article