我正试图在用户界面上显示一个ProgressDialog 5秒钟,以阻止UI,这样我的locationClient就可以在5秒后连接起来。
我正在取消对话框box.But,对话框根本没有显示,移动屏幕显示为3-4 seconds.Is,这是因为我在UiThread上运行这段代码?如果是的话,我可以遵循其他任何方法,停止5秒,然后执行挂起的代码。
我的线程代码如下:
public void startConnecting(final boolean isSearchWarnings) {
mProgressDialog = ProgressDialog.show(this, "Please wait","Long operation starts...", true);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 5; i++) {
try {
if (mLocationClient.isConnected()) {
break;
} else {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mProgressDialog.dismiss();
if (isSearchWarnings) {
new getWarnings().execute(false);
} else {
new getWarnings().execute(true);
}
}
});
}发布于 2013-09-11 13:28:39
你有这个
MainActivity.this.runOnUiThread(new Runnable()然后
Thread.sleep(1000);您正在调用ui线程上的睡眠。这将阻止您不应该执行的ui线程。你会得到ANR。
http://developer.android.com/training/articles/perf-anr.html
如果您想造成延迟,请使用handler
https://stackoverflow.com/questions/18742523
复制相似问题