扩展 AsyncTask是如何实现的?工作
我想知道它是如何工作的。我已经阅读了文档几次,但不明白这个想法。
我注意到在某些情况下 onPostExecute 不执行任何操作。
谁能给我一些解释吗?
I would like to know how it works. I have read the documentation a couple of times but don’t get the idea.
I noticed that in some cases onPostExecute
does nothing.
Can anyone please give me a little explanation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AsyncTask
是一个从Thread
派生的类,它为您提供了一种在后台执行某些操作的简单而正确的方法,并且能够通知UI Thread
>。为了使用它,您应该创建一个
类
,它扩展
它并定义类型参数。它们是Params
、Progress
和Result
。在此处了解更多信息。onPostExecute()
是当doInBackground()
完成执行时调用的方法,但onPostExecute()
是在 UI 线程上运行的。因此,您可以通知 UI 工作已完成。您可以在此处查看
AsyncTask
用法示例。AsyncTask
is a class derived fromThread
and provides you a simple and proper way of doing some things in the background with the ability of notifying theUI Thread
.In order to use it you should create a
class
, whichextends
it and define the type parameters. They areParams
,Progress
andResult
. Read about them more here.onPostExecute()
is a method which is called whendoInBackground()
finished it's execution, butonPostExecute()
is run on the UI Thread. So, you can notify the UI about the work has been done.You can see an example of
AsyncTask
usage here.
部分在 Java 中称为 泛型。它用于那些原始编码器希望使用它的类,无论“最终用户”将选择什么类型。通常,当您想要启动/停止进度对话框时,您可以使用
onPre/PostExecute()
。如果您想在没有进度对话框的情况下在进度期间更新 UI,您可以在
onProgressUpdate()
中执行操作(即:您从网络加载图像并在加载时显示它们)如果您有带有进度条的对话框,您必须使用所有这些来更新进度条。
The
<Void, Void, Long>
part is what in Java is called Generics. It is used in those classes where the original coder wants it to be used no matter what the types the "end user" will choose.Typically you use
onPre/PostExecute()
when you want to start/stop a progress dialog.If you want to update the UI during the progress without a progress dialog you can do things in
onProgressUpdate()
(i.e.: you're loading images from web and displaying them as they get loaded)If you have a dialog with progress bar you'll have to use all of them to update the progress bar.
这篇文章用图表很好地解释了 AsyncTask 概念!
This post explains AsyncTask concept nicely with diagram !
当您想要在完成后台任务
{(doInBackground())}
后执行某些操作时,请使用{onPostExecute()}
。例如,
{onPostExecute()}
is used when you want to do something after completing your background task{(doInBackground())}
.For example,