什么样的参数传递到AsyncTask的< ARG1,ARG2,ARG3>?参数、LT、AsyncTask、GT

2023-09-11 12:32:38 作者:因帅被判无妻

我不明白我应该摆在这里,并在这里这些争论结束了?究竟应该怎么放,并在那里究竟会去了?我是否需要包括所有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?

推荐答案

谷歌的Andr​​oid文档说:

Google's Android Documentation Says that :

这是异步任务由3泛型类型定义,所谓的PARAMS,进展和成效,以及4个步骤,呼吁preExecute,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's博客

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对象的数组(你在标题中看到了什么?我们有......延伸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 - Indicates that the task is running.

待 - 表示该任务尚未执行

PENDING - Indicates that the task has not been executed yet.

完成 - 表示onPostExecute(Z)已完成

FINISHED - Indicates that onPostExecute(Z) has finished.

关于使用AsyncTask的提示的

Hints about using AsyncTask

不要呼吁preExecute,doInBackground和onPostExecute手动的方法。这是由系统自动完成。

Do not call the methods onPreExecute, doInBackground and onPostExecute manually. This is automatically done by the system.

您不能调用在另一个AsyncTask的或线程的AsyncTask的。该方法的调用execute必须在UI线程来完成。

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线程(在这里,你可以调用另一个AsyncTask的!)。执行的方法

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.

 
精彩推荐
图片推荐