AsyncTask与ProgressDialog,对话框闪烁



我有一个AsyncTask叫做QueueTask,我的一个活动调用。它应该在显示进度对话框时执行一些操作。在AsyncTask完成后,一个意图被创建,一个新的布局和活动成为焦点。

问题是对话框快速闪过,然后调用意图。在执行操作时,对话框应该保持不变,但事实并非如此。为什么它粘不住呢?

注意:doInBackground需要大约300秒来执行。

从activity调用:

QueueTask task = new QueueTask(date, roomNum, time, fieldInputUser, fieldInputCardNum);
task.execute();
Intent intent = new Intent(this, DatePickerActivity.class);
this.startActivity(intent);

在活动类中定义的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 ()

onPostExecute()方法中调用startActivity()

相关内容

  • 没有找到相关文章

最新更新