Android 中的异步任务
任何人都可以告诉有关 android 应用程序中使用的“asynctask”吗?目前我正在开发一个应用程序,在该应用程序中我必须创建一个类,在该类中我必须获取给定的任何特定 URL 的响应。
在这个特定的课程中,我被告知要使用“asynctask”来执行此任务。到目前为止,我的所有问题都得到了非常快速的答复,我非常感谢自从我在这里发布第一个问题以来所有帮助我的人。
我对 Android 编程相当陌生,并且对社区对我的伪装感到有点信心。
谢谢, 大卫
Can anybody tell about the "asynctask" used in android application. Currently I am working on an application where I have to create a Class in which I have to just get the response of any particular URL given.
I this particular class I was told to perform this task by making use of "asynctask". I had been getting very quick responses of all my Questions from here so far and I am greatly obliged to all of them who are helping me since my first question I posted here.
I am quite new to Android Programming and feeling a bit confidence by the community's camouflage with me.
Thanks,
david
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Google 的
AsyncTask
文档非常出色:http:// developer.android.com/reference/android/os/AsyncTask.html它基本上是一个使线程变得非常简单的构造。当您执行诸如向某个 URL 发出 Web 请求之类的操作时,您不希望阻塞 UI 线程。但是,您通常希望在后台任务完成后用结果更新 UI。
AsyncTask
让这变得简单。首先,您需要创建一个扩展AsyncTask
的类。当您执行
任务时,将在后台线程上调用其doInBackground
方法。您可以在此处从网络下载某些内容或执行任何其他您需要执行的操作。doInBackground
的返回值将提供给onPostExecute
方法,该方法可以适当地更新 UI。再次,我建议您查看 Google 的文档。他们有关于如何使用它的很好的例子。
Google's documentation for
AsyncTask
is pretty excellent: http://developer.android.com/reference/android/os/AsyncTask.htmlIt's basically a construct that makes threading very simple. When you are doing something like making a web request to some URL, you don't want to block the UI thread. However, you usually want to update your UI with the results once your background task has completed.
AsyncTask
makes this easy. First, you need to create a class that extendsAsyncTask
. When youexecute
the task, itsdoInBackground
method will be called on a background thread. This is where you can download something from the Web or do whatever else you need to do.The return value from
doInBackground
will be provided to theonPostExecute
method, which can update the UI appropriately.Again, I recommend checking out Google's documentation. They've got great examples on how to use this.