进度对话框不会出现 - Android
我想在我的应用程序中创建一个进度对话框,以便在发送一些信息时使用。但我写的代码无法运行。如果方法 send() 执行,但对话框永远不会出现,因为它很快就会消失
这是我的代码:
ProgressDialog myProgressDialog = ProgressDialog.show(Tents.this,
"Please wait...", "Sending...", true);
send();
myProgressDialog.dismiss();
goFour();
如何使对话框持续时间更长一点?
I want to make a Progress Dialog Box in my app to use when sending some information. But the code I wrote won't work. It the method send() executes but the dialog box never appears because it dismisses very quickly
Here is my code :
ProgressDialog myProgressDialog = ProgressDialog.show(Tents.this,
"Please wait...", "Sending...", true);
send();
myProgressDialog.dismiss();
goFour();
How do I make the Dialog Box Last a little longer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先 - 您不应该在与 show() 和 Dismiss() 相同的线程中执行 send() - 因为您在发送过程中实际上阻塞了 UI 线程。该对话框实际上永远不会显示 - 因为为了在调用 show() 后显示它,您需要将控制权交还给 UI 线程中的主循环程序,然后简单地完成处理您正在处理的任何事件。否则 UI 线程将永远没有机会绘制对话框。
最好的想法是开始在 AsyncTask 中运行 send() 并在 onPostExecute() 中调用 Dismiss() (请参阅 http://developer.android.com/reference/android/os/AsyncTask.html 了解如何运行异步任务)。
First of all - you should not do send() in the same thread as show() and dismiss() - because you are effectively blocking UI thread during sending. The dialog will actually never show - because in order to show it after show() is called, you need to give the control back to the main looper in UI thread and simply finish handling whatever event you are handling. Otherwise the UI thread will never have a chance to draw your dialog.
The best idea is to start running send() in AsyncTask and call dismiss() in onPostExecute() (see http://developer.android.com/reference/android/os/AsyncTask.html to get idea how to run async task).
您可能会收到一个进度对话框,但它会立即关闭,因为它没有什么可等待的。
对于我的示例,我会假装您希望在 OnCreate 中执行此操作:
编辑:如果它仍然立即消失,请确保
send();
执行实际上需要一些时间的操作。 ;)You are probably getting a progress dialog, but having it immediately dismiss as it has nothing to wait for.
I'll pretend you want this in OnCreate for my example:
EDIT: If it still goes away immediately, make sure
send();
does something that actually takes some time. ;)UI线程用于启动send(),这将不起作用并且不会显示进度对话框。
在另一个线程或 AsynTask doBackground 中调用 send 并在完成后关闭对话框。
The UI thread is used to start send() , this will not work and progress dialog will not be shown .
Call send in another thread or AsynTask doBackground and on completion dismiss the dialog.
如果您的发送操作完成得太快,以至于对话框无法正确显示,我是否建议您通过
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
在活动的右上角使用不确定的进度条,然后使用setProgressBarInminatedVisibility(true/false)
。If your send action is completing so quickly that the dialog is not displaying properly, might I suggest instead using an indeterminate progress bar in the upper right corner of your activity via
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
and then utilizingsetProgressBarIndeterminateVisibility(true/false)
.“我想我真正的问题是如何让它持续更长时间?”我的回答是为什么???!!!
我认为你最好显示一个警报对话框来确认你的发送功能已完成,这会让用户无缘无故地等待会很烦人!
"I guess my real question would then be how do I make it so that it lasts a little longer?" My answer would be WHY???!!!
I think you would be better showing an alert dialog to confirm your send function has completed, it would be annoying for the user having to wait for no reason!