What arguments are passed into AsyncTasklt;arg1, arg2, arg3gt;?(哪些参数被传递到 AsyncTaskarg1、arg2、arg3?)
问题描述
我不明白我应该在这里输入什么以及这些争论的最终结果是什么?我到底应该放什么,它到底会去哪里?我需要包括所有 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?
推荐答案
Google 的 Android 文档说:
Google's Android Documentation Says that :
异步任务由 3 个通用类型定义,称为 Params、Progress 和 Result,以及 4 个步骤,称为 onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute.
An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
AsyncTask 的泛型类型:
AsyncTask's generic types :
异步任务使用的三种类型如下:
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
并非所有类型都始终被异步任务使用.要将类型标记为未使用,只需使用类型 Void:
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
您可以进一步参考:http://developer.android.com/reference/android/os/AsyncTask.html
或者您可以通过参考 Sankar-Ganesh 的博客
private class MyTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){
}
此方法在启动新线程之前执行.没有输入/输出值,因此只需初始化变量或您认为需要做的任何事情.
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.
protected Z doInBackground(X...x){
}
AsyncTask 类中最重要的方法.您必须将您想要在后台执行的所有内容放在与主线程不同的线程中.这里我们有一个类型为X"的对象数组作为输入值(你在标题中看到了吗?我们有...extends AsyncTask"这些是输入参数的类型)并从该类型返回一个对象Z".
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".
protected void onProgressUpdate(Y y){
}
此方法使用方法 publishProgress(y) 调用,通常在您想在主屏幕中显示任何进度或信息时使用,例如在后台显示您正在执行的操作进度的进度条.
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.
protected void onPostExecute(Z z){
}
后台操作完成后调用该方法.作为输入参数,您将收到 doInBackground 方法的输出参数.
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.
X、Y 和 Z 类型呢?
从上面的结构可以推断:
As you can deduce from the above structure:
X – The type of the input variables value you want to set to the background process. This can be an array of objects.
Y – The type of the objects you are going to enter in the onProgressUpdate method.
Z – The type of the result from the operations you have done in the background process.
我们如何从外部类调用此任务?只需以下两行:
How do we call this task from an outside class? Just with the following two lines:
MyTask myTask = new MyTask();
myTask.execute(x);
其中 x 是 X 类型的输入参数.
Where x is the input parameter of the type X.
一旦我们的任务运行起来,我们就可以从外部"找到它的状态.使用getStatus()"方法.
Once we have our task running, we can find out its status from "outside". Using the "getStatus()" method.
myTask.getStatus();
我们可以收到以下状态:
and we can receive the following status:
RUNNING - 表示任务正在运行.
RUNNING - Indicates that the task is running.
PENDING - 表示任务尚未执行.
PENDING - Indicates that the task has not been executed yet.
FINISHED - 表示 onPostExecute(Z) 已完成.
FINISHED - Indicates that onPostExecute(Z) has finished.
关于使用 AsyncTask 的提示
不要手动调用 onPreExecute、doInBackground 和 onPostExecute 方法.这是系统自动完成的.
Do not call the methods onPreExecute, doInBackground and onPostExecute manually. This is automatically done by the system.
您不能在另一个 AsyncTask 或线程中调用 AsyncTask.方法execute的调用必须在UI Thread中完成.
You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread.
onPostExecute 方法在 UI Thread 中执行(这里可以调用另一个 AsyncTask!).
The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!).
任务的输入参数可以是一个Object数组,这样你就可以放任何你想要的对象和类型.
The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.
这篇关于哪些参数被传递到 AsyncTask<arg1、arg2、arg3>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:哪些参数被传递到 AsyncTask<arg1、arg2、arg3>?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01