我有一个名为 QueueTask 的 AsyncTask,我的一个活动调用它。它应该在显示进度对话框时执行一些操作。在 AsyncTask 完成后,一个意图被创建并且一个新的布局和活动成为焦点。
问题是对话框快速闪烁,然后调用了意图。在执行操作时,对话框应该保持不变,但它不会。为什么不粘?
注意:doInBackground 应该需要大约 300 秒来执行。
来自活动的调用:
QueueTask task = new QueueTask(date, roomNum, time, fieldInputUser, fieldInputCardNum);
task.execute();
Intent intent = new Intent(this, DatePickerActivity.class);
this.startActivity(intent);
Activity 类中定义的 AsyncTask:
public class QueueTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
private Library lib;
private String date;
private int room;
private int time;
private String user;
private String id;
public QueueTask(String date, int room, int time, String user, String id) {
this.date = date;
this.room = room;
this.time = time;
this.user = user;
this.id = id;
}
@Override
protected void onPreExecute() {
lib = new Library();
progressDialog = ProgressDialog.show(ManageRoomActivity.this, "Queuing", "Queuing process");
}
@Override
protected Void doInBackground(Void... params) {
int timer = 300;
while(timer > 0) {
if(lib.roomAvailable(date, room, time)) {
lib.bookRoom(user, date, room, id, time);
return null;
}
android.os.SystemClock.sleep(1000);
timer --;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
}
通过放置固定:
Intent intent = new Intent(ManageRoomActivity.this, DatePickerActivity.class);
ManageRoomActivity.this.startActivity(intent);
进入 onPostExecute()