2013-07-24 86 views
3

我想在後臺做一個簡單的任務,並顯示進度條,而它正在完成。ProgressBar沒有出現在AsyncTask

這是主要的代碼(和唯一的)活性:

public class Login extends Activity { 

    public static ProgressDialog progressDialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     (...) 
     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
     progressDialog = new ProgressDialog(activity); 
     progressDialog.setMessage("Sending data..."); 
     progressDialog.setCancelable(false); 
     (...) 
      // In some onClick Eevent.. 
      JSONObject result = new Urltasks().execute(...).get(); 
     (...) 

    } 
} 

這是活動的代碼:

class Urltasks extends AsyncTask<String, Integer, JSONObject>{ 

    protected void onPreExecute() { 
     System.out.println("Inicia onPreExecute"); 
     Login.progressDialog.show(); 
    } 

    protected void onPostExecute(String result) { 
     Login.progressDialog.dismiss(); 
    } 

    protected JSONObject doInBackground(String... arg0) { 
      // Some work being done. I do not use Login.progressDialog here 
    } 
} 

利用該代碼ProgressDialog時顯示該任務結束,它不會解僱。

回答

0

不要把你的進度對話框放到你的課堂上。保持它在異步任務

這是你如何展示你的對話

 progressDialog = ProgressDialog.show(yourActivityHERE.this, "", 
    "Loading... please wait."); 

編輯這個給你:

class Urls extends AsyncTask<String, Integer, JSONObject>{ 

ProgressDialog progressDialog; 

protected void onPreExecute() { 

    progressDialog = ProgressDialog.show(yourActivityHERE.this, "", 
    "Loading... please wait."); 

} 

protected void onPostExecute(String result) { 
    progressDialog.dismiss(); 
} 

protected JSONObject doInBackground(String... arg0) { 
     // Some work being done. I do not use Login.progressDialog here 
} 
} 
+0

什麼時候應該構建它呢?在'OnPreExcute,對嗎?用哪個上下文? –

+0

是OnPreExcute,並且上下文是當前正在進行的活動..我已經爲您編輯了我的答案 – NightSkyCode

0

創建您的Urltasks發送調用類的上下文中的構造函數。 然後用此背景下創建的preExecute進度對話框你Urltasks

class Urltasks extends AsyncTask<String, Integer, JSONObject>{ 
Context mContext; 
public static ProgressDialog progressDialog; 

public UrlTasks(Context c){ 
    mContext=c; 
} 
protected void onPreExecute() { 
    progressDialog = new ProgressDialog(mContext); 
    progressDialog.setMessage("Sending data..."); 
    progressDialog.setCancelable(false); 
    progressDialog.show(); 
} 

protected void onPostExecute(String result) { 
    progressDialog.dismiss(); 
} 

protected JSONObject doInBackground(String... arg0) { 
     // Do your work here 
} 

}

2

問題是在這裏的:

JSONObject result = new Urltasks().execute(...).get(); 

調用上AsyncTaskget()當前線程,即你的UI線程,直到AsyncTask被執行。因此進度對話框不能運行。刪除get()

要獲得AsyncTask的結果,您可以通過在偵聽器回調到的AsyncTask是得到通知時,結果將可用:

class YourAsyncTask extends AsyncTask<ParamType, ProgressType, ResultType> { 

    private YourResultListener mListener; 

    interface YourResultListener { 
    void onResultAvailable(ResultType result); 
    } 

    YourAsyncTask(YourResultListener listener) { 
    mListener = listener; 
    } 

    @Override protected ResultType doInBackground(ParamType... params) { 
    //... 
    } 

    @Override protected void onPostExecute(ResultType result) { 
    mListener.onResultAvailable(result); 
    } 
} 

你可以用它喜歡:

mProgressDialog.show(); 
new YourAsyncTask(new YourResultListener() { 
    @Override void onResultAvailable(ResultType result) { 
     mProgressDialog.dismiss(); 
     // use result 
    }).execute(params); 

個人而言,我喜歡保持用戶界面元素,如進度對話框脫鉤來自異步任務。