扩展 AsyncTask是如何实现的?工作

发布于 2024-11-01 10:48:28 字数 100 浏览 1 评论 0原文

我想知道它是如何工作的。我已经阅读了文档几次,但不明白这个想法。

我注意到在某些情况下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2024-11-08 10:48:28

AsyncTask 是一个从 Thread 派生的类,它为您提供了一种在后台执行某些操作的简单而正确的方法,并且能够通知 UI Thread >。

为了使用它,您应该创建一个,它扩展它并定义类型参数。它们是ParamsProgressResult。在此处了解更多信息。

onPostExecute() 是当 doInBackground() 完成执行时调用的方法,但 onPostExecute() 是在 UI 线程上运行的。因此,您可以通知 UI 工作已完成。

您可以在此处查看 AsyncTask 用法示例。

AsyncTask is a class derived from Thread and provides you a simple and proper way of doing some things in the background with the ability of notifying the UI Thread.

In order to use it you should create a class, which extends it and define the type parameters. They are Params, Progress and Result. Read about them more here.

onPostExecute() is a method which is called when doInBackground() finished it's execution, but onPostExecute() 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.

最终幸福 2024-11-08 10:48:28

部分在 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.

一杯敬自由 2024-11-08 10:48:28

这篇文章用图表很好地解释了 AsyncTask 概念!

This post explains AsyncTask concept nicely with diagram !

叶落知秋 2024-11-08 10:48:28

当您想要在完成后台任务{(doInBackground())}后执行某些操作时,请使用{onPostExecute()}

例如,

start Progress Bar @ {onPreExecute()}
running Progress Bar @ {doInBackground()}
stop Progress Bar @ {onPostExecute()}

{onPostExecute()} is used when you want to do something after completing your background task {(doInBackground())}.

For example,

start Progress Bar @ {onPreExecute()}
running Progress Bar @ {doInBackground()}
stop Progress Bar @ {onPostExecute()}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文