![](/img/trans.png)
[英]Android: Handling SocketTimeoutException inside AsyncTask with a simple Alert Dialog
[英]Confirm dialog (Alert) inside AsyncTask
我在AsyncTask中运行了很长的过程,但是在处理过程中可能需要确认用户的某些信息。 我知道如何显示确认对话框,但是如何检索输出并保持等待直到使用确认?
this.runOnUiThread(new Runnable() {
public void run() {
boolean output = ConfirmUser(message);
}
});
我会说这是个坏主意。 如果需要用户的确认,则最好将AsyncTask分为两部分:首先做一部分,然后在onPostExecute()内显示对话框(因为它在ui线程上运行),然后根据用户的操作启动第二个AsyncTask 。
如果仍然要执行一个AsyncTask,则可以这样进行:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.