我想在後臺做一個簡單的任務,並顯示進度條,而它正在完成。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時顯示該任務結束,它不會解僱。
什麼時候應該構建它呢?在'OnPreExcute,對嗎?用哪個上下文? –
是OnPreExcute,並且上下文是當前正在進行的活動..我已經爲您編輯了我的答案 – NightSkyCode