对于啰嗦的标题我很抱歉 - 我不确定如何正确地描述它。我想(在我的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
}