哪些参数被传递到 AsyncTask<arg1, arg2, arg3>? [英] What arguments are passed into AsyncTask&lt;arg1, arg2, arg3&gt;?

查看:29
本文介绍了哪些参数被传递到 AsyncTask<arg1, 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 的提示

  1. 不要手动调用方法 onPreExecute、doInBackground 和 onPostExecute.这是由系统自动完成的.

  1. 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!).

任务的输入参数可以是一个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>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆