线程和进度对话框
我正在开发我的第一个 Android 应用程序,当我想要显示 ProgressDialog 以指示进程正在运行时,我遇到了问题。 在我的应用程序中,用户通过按下按钮来触发耗时的任务。当用户按下按钮时,将调用“OnClickListener”的“OnClick”函数。在此函数中,这是我当前正在做的事情:
- creation and configuration of an instance of the ProgressDialog class,
- creation of a thread dedicated to the time consuming task,
- attempt to display the ProgressDialog using the "show" method,
- start of the thread,
- main Activity suspended (call of the "wait" function)
- wake up of the main Activity by the thread when it is finished
- removal of the ProgressDialog by calling the "dismiss" function.
一切正常(长任务的结果是正确的),但 ProgressDialog 永远不会出现。我做错了什么?
预先感谢您花时间帮助我。
I am developing my first Androïd application and I'm facing a problem when I want to display a ProgressDialog to indicate that a process is being run.
In my application, the user triggers a time consuming task by pressing a Button. The "OnClick" function of my "OnClickListener" is called when the user presses the Button. In this function, here is what I'm currently doing :
- creation and configuration of an instance of the ProgressDialog class,
- creation of a thread dedicated to the time consuming task,
- attempt to display the ProgressDialog using the "show" method,
- start of the thread,
- main Activity suspended (call of the "wait" function)
- wake up of the main Activity by the thread when it is finished
- removal of the ProgressDialog by calling the "dismiss" function.
Everything works fine (the result of the long task is correct) but the ProgressDialog nevers appears. What am I doing wrong?
Thanks in advance for the time you will spend trying to help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该对主 Activity/UI 线程调用
wait()
,因为这实际上会冻结包括您的 ProgressDialog 在内的 UI,因此它没有时间淡入并且永远不会显示。尝试正确使用多线程: http://developer.android.com/resources/articles /painless-threading.html
You should not call
wait()
to the Main Activity/UI thread, because this is actually freezing the UI including your ProgressDialog, so it has no time to fade in and will never be shown.Try to use multithreading correctly: http://developer.android.com/resources/articles/painless-threading.html
我建议使用
AsyncTask
,因为它的目的正是为了处理此类问题。有关如何使用它的说明,请参阅此处。请注意,Floern 答案中的链接页面还建议使用AsyncTask
。您需要执行以下操作:
AsyncTask
onPreExecute()
方法来创建并显示ProgressDialog
。 (您可以在子类的成员中保存对它的引用)onPostExecute()
方法来隐藏对话框。execute()
。如果您将子类设置为活动的内部类,您甚至可以使用活动的所有成员。
I would suggest using
AsyncTask
, as it's purpose is exactly to handle this kind of problem. See here for instructions how to use it. Note that the linked page in Floern's answer also recommends the use ofAsyncTask
.You would need to do the following:
AsyncTask
onPreExecute()
method to create and show aProgressDialog
. (You could hold a reference to it in a member of your subclass)doInBackground()
method to execute your time consuming action.onPostExecute()
method to hide your dialog.execute()
on it.If you make your subclass an inner class of your activity, you can even use all of your activity's members.