對於囉嗦的標題我很抱歉 - 我不確定如何正確地描述它。我想(在我的onCreate()方法):顯示進度對話框+暫停代碼執行時在後臺加載
-Initialize一些事情
-display進度對話框,而我從我的後端
加載一些數據-clear進度對話框,然後繼續與代碼
我明白了典型的進度對話框的解決方案是一個asynctask,我試過使用(見下文)。但是,這並不會像我想要的那樣鎖定代碼執行。在lwpd.execute()之後的代碼依賴於已經發生的加載。我是否在過度複雜?什麼是做我想做的正確方法?
僅供參考,我的AsyncTask實現:
public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{
private ProgressDialog pd; //the progress dialog
private String title; //the title of the progress dialog
private String message; //the body of the progress dialog
private Runnable task; //contains the code we want to run in the background
private Context c;
public LoadWithProgressDialog(Context context,String t, String m,Runnable r){
super();
c = context;
task = r;
title = t;
message = m;
}
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(c,title, message, false, false);
}
@Override
protected Boolean doInBackground(Void... params) {
task.run();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
pd.dismiss();
}
}
和
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize variables
LoadWithProgressDialog lwpd = new LoadWithProgressDialog(this,"Loading","Loading Truck Data", new Runnable() {
public void run(){
//code that loads things
}
});
lwpd.execute();
//continue on with my code
}