哪些参数被传递到 AsyncTask中?
我不明白我应该在这里写什么以及这些争论最终会在哪里结束?我到底应该放什么,它到底会放在哪里?我需要包含全部 3 个还是可以包含 1、2、20?
I don't understand what I am supposed to put in here and where these arguments end up? What exactly should I put, and where exactly will it go? Do I need to include all 3 or can I include 1,2,20?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Google 的 Android 文档指出:
异步任务由 3 个通用类型(称为 Params、Progress 和 Result)和 4 个步骤(称为 onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute)定义。
AsyncTask 的通用类型:
异步任务使用的三种类型如下:
并非所有类型都始终由异步任务使用。要将类型标记为未使用,只需使用类型 Void:
您可以进一步参考:http ://developer.android.com/reference/android/os/AsyncTask.html
或者您可以参考Sankar-Ganesh 的博客
典型的 AsyncTask 类的结构如下:
该方法在之前执行启动新线程。没有输入/输出值,因此只需初始化变量或您认为需要执行的任何操作。
AsyncTask 类中最重要的方法。您必须将您想要在后台执行的所有操作放置在与主线程不同的线程中。这里我们有一个来自类型“X”的对象数组作为输入值(你在标题中看到了吗?我们有“...extends AsyncTask”这些是输入参数的类型)并从该类型返回一个对象“Z”。
使用publishProgress(y)方法调用此方法,通常在您想要在主屏幕中显示任何进度或信息时使用它,例如显示您在后台执行的操作进度的进度条。
该方法在后台操作完成后调用。作为输入参数,您将收到 doInBackground 方法的输出参数。
X、Y 和 Z 类型怎么样?
从上面的结构中可以推断出:
我们如何从外部类中调用这个任务?只需以下两行:
其中x是X类型的输入参数。
一旦我们的任务运行,我们就可以从“外部”找到它的状态。使用“getStatus()”方法。
我们可以收到以下状态:
RUNNING - 表示任务正在运行。
PENDING - 表示任务尚未执行。
FINISHED - 表示 onPostExecute(Z) 已完成。
有关使用 AsyncTask 的提示
不要手动调用 onPreExecute、doInBackground 和 onPostExecute 方法。这是系统自动完成的。
您不能在另一个 AsyncTask 或线程内调用 AsyncTask。方法execute 的调用必须在UI 线程中完成。
onPostExecute 方法在 UI Thread 中执行(这里可以调用另一个 AsyncTask!)。
任务的输入参数可以是一个Object数组,这样你可以放任何你想要的对象和类型。
Google's Android Documentation Says that :
An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
AsyncTask's generic types :
The three types used by an asynchronous task are the following:
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
You Can further refer : http://developer.android.com/reference/android/os/AsyncTask.html
Or You Can clear whats the role of AsyncTask by refering Sankar-Ganesh's Blog
Well The structure of a typical AsyncTask class goes like :
This method is executed before starting the new Thread. There is no input/output values, so just initialize variables or whatever you think you need to do.
The most important method in the AsyncTask class. You have to place here all the stuff you want to do in the background, in a different thread from the main one. Here we have as an input value an array of objects from the type “X” (Do you see in the header? We have “...extends AsyncTask” These are the TYPES of the input parameters) and returns an object from the type “Z”.
This method is called using the method publishProgress(y) and it is usually used when you want to show any progress or information in the main screen, like a progress bar showing the progress of the operation you are doing in the background.
This method is called after the operation in the background is done. As an input parameter you will receive the output parameter of the doInBackground method.
What about the X, Y and Z types?
As you can deduce from the above structure:
How do we call this task from an outside class? Just with the following two lines:
Where x is the input parameter of the type X.
Once we have our task running, we can find out its status from “outside”. Using the “getStatus()” method.
and we can receive the following status:
RUNNING - Indicates that the task is running.
PENDING - Indicates that the task has not been executed yet.
FINISHED - Indicates that onPostExecute(Z) has finished.
Hints about using AsyncTask
Do not call the methods onPreExecute, doInBackground and onPostExecute manually. This is automatically done by the system.
You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread.
The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!).
The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.
我参加聚会已经太晚了,但我认为这可能会对某人有所帮助。
< /a>
I'm too late to the party but thought this might help someone.
保持简单!
AsyncTask
是在后台线程中运行的后台任务。它接受输入,执行进度并给出输出。在我看来,混乱的主要根源在于我们尝试记住
AsyncTask
中的参数。关键是不要死记。
如果您可以想象您的任务真正需要做什么,那么使用正确的签名编写
AsyncTask
将是小菜一碟。只需弄清楚您的输入、进度和输出是什么,您就可以开始了。
例如:
AsyncTask 的核心!
doInBackgound()
方法是 AsyncTask 中最重要的方法,因为AsyncTask
参数而变化。显示代码< /strong>
那么我该如何编写 DownloadTask 的代码呢?
您将如何运行此任务
Keep it simple!
An
AsyncTask
is background task which runs in the background thread. It takes an Input, performs Progress and gives Output.In my opinion the main source of confusion comes when we try to memorize the parameters in the
AsyncTask
.The key is Don't memorize.
If you can visualize what your task really needs to do then writing the
AsyncTask
with the correct signature would be a piece of cake.Just figure out what your Input, Progress and Output are and you will be good to go.
For example:
Heart of the AsyncTask!
doInBackgound()
method is the most important method in anAsyncTask
becauseAsyncTask
parameters.Show me the code
So how will I write the code for DownloadTask?
How will you run this Task
请参阅以下链接:
您不能传递超过三个参数,如果您只想传递 1 个参数,则对其他两个参数使用 void 。
异步任务由在后台线程上运行的计算定义,其结果在 UI 线程上发布。异步任务由 3 个通用类型(称为 Params、Progress 和 Result)和 4 个步骤(称为 onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute)定义。
Refer to following links:
You cannot pass more than three arguments, if you want to pass only 1 argument then use void for the other two arguments.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
简而言之,AsyncTask中有3个参数
DoInBackground(String... params) 中使用的输入参数
OnProgressUpdate(String... status) 中使用的显示进度状态的参数
OnPostExcute(String... result) 中使用结果的参数
注意: - [参数类型可以根据您的要求而变化]
in Short, There are 3 parameters in AsyncTask
parameters for Input use in DoInBackground(String... params)
parameters for show status of progress use in OnProgressUpdate(String... status)
parameters for result use in OnPostExcute(String... result)
Note : - [Type of parameters can vary depending on your requirement]