等待线程完成时显示进度对话框 2 秒
我有一个活动,其中我调用一个可能需要一段时间才能完成的服务,并且在该服务未完成之前,单击的菜单选项应该返回一条错误消息,例如“数据尚未准备好,请尽快重试” 但是,我想在抛出该错误之前给它几秒钟的时间来完成,在此期间,我想在屏幕上显示一个进度对话框。 这是我的代码:
if(calculatedValue.equals(NOT_YET_CALCULATED)){
//show progress dialog
ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
long timeStarted = System.currentTimeMillis();
while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
// wait for at most 1.5 seconds
}
progress.dismiss();
if(calculatedValue.equals(NOT_YET_CALCULATED)){
//if it's still not there, there's probably a problem, show generic alert dialog
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle(R.string.not_yet_calulated_alert_title);
dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
dialog.show();
}else{
startActivity(j);
}
}else{
startActivity(j);
}
让我解释一下: 我检查该值是否存在,如果不存在,我会显示一个进度图标(我要寻找圆圈)1.5 秒。如果在那之后它仍然不存在,我会放弃并显示一条错误消息。
问题是进度内容没有显示在屏幕上。 我做错了什么?
谢谢, e.
I have an activity in which I call a service that can take a while to complete and until that service didn't finish, the menu options that are clicked should return an error message like "data not yet ready, please try again soon"
however, I want to give it a couple of seconds to finish before I throw that error and during that time, I want to show a progressdialog on the screen.
here is the code that I have:
if(calculatedValue.equals(NOT_YET_CALCULATED)){
//show progress dialog
ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
long timeStarted = System.currentTimeMillis();
while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
// wait for at most 1.5 seconds
}
progress.dismiss();
if(calculatedValue.equals(NOT_YET_CALCULATED)){
//if it's still not there, there's probably a problem, show generic alert dialog
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setTitle(R.string.not_yet_calulated_alert_title);
dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
dialog.show();
}else{
startActivity(j);
}
}else{
startActivity(j);
}
Let me explain:
I check if the value exists, and if it doesn't I show a progress icon (i'm going for the circle thingy) for 1.5 seconds. If after that time it's still not there, I give up and show an error message.
The problem is that the progress thing does not show up on the screen.
what am I doing wrong?
thanks,
e.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了,应该为此使用异步任务,代码如下:
figured it out, should use async task for this, here's the code:
你现在所做的至少会导致僵局。
我通常做的是创建进度对话框,就像您现在所做的那样,同时启动另一个线程,在这种情况下等待 1.5 秒,然后停止进度对话框。
例如
,为了开始,只需在创建进度对话框后调用上面的可运行程序
What you are making now atleast causes a deadlock.
What I normally do is creating the progressdialog as you are doing now and on the same time start another thread that in this situation waits for 1.5second and stop the progressdialog then.
For example
And for starting just call the above runnable after creating the progressdialog