[英]How to pause the thread for some time and then show the UI once user interacts, continue the thread execution
我已经启动了线程,在该线程中我正在尝试连接到服务器,在收到响应后,我必须使用事件侦听器(通过接口实现)更新UI。 在接收到响应后,我需要在用户单击“确定”后显示弹出对话框,需要继续该线程并完成其他进程。
class ConnectionThread extends Thread {
ConnectionThread() {
this.setName("ConnectionThread");
}
@Override
public void run() {
// Need to pause the thread for sometime, Need to do the functionality here.
((Activity)mContext).runOnUiThread(new Runnable() {
public void run() {
// custom dialog
showAlertDialog();
// start the thread functionality again from that position.
}
});
}
我已尝试使用wait()概念并加入,这些都没有按预期帮助。 任何帮助赞赏。
你可以使用countdownlatch
class ConnectionThread extends Thread {
CountDownLatch countDownLatch = new CountDownLatch(1);
public ConnectionThread() {
this.setName("ConnectionThread");
}
@Override
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
//update ui then
countDownLatch.countDown();
}
});
countDownLatch.await();
//start process again
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.