简体   繁体   中英

Confirm dialog (Alert) inside AsyncTask

I have a long process running inside AsyncTask but it might need to confirm something from user while processing. I know how I can show a confirm dialog but how can I retrieve the output and keep wait till use confirms?

this.runOnUiThread(new Runnable() {
            public void run() {
               boolean output = ConfirmUser(message);
            }
        });

I will say that it is bad idea. If you need a confirmation from user, you better split your AsyncTask in two parts: do some part first, then inside onPostExecute() you can show dialog (because it is running on ui thread) and, depending on user action, launch second AsyncTask.

If you still want to do it one AsyncTask, you can do it like this:

final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<Boolean>(1);
this.runOnUiThread(new Runnable() {
    public void run() {
        // Assuming you have ConfirmUser method which returns boolean
        queue.add(ConfirmUser(message));
    }
});


Boolean result = null;
try {
    // This will block until something will be added to the queue
    result = queue.take();
} catch (InterruptedException e) {
    // deal with it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM