哪些参数被传递到 AsyncTask中?

发布于 2024-11-08 11:50:13 字数 78 浏览 0 评论 0原文

我不明白我应该在这里写什么以及这些争论最终会在哪里结束?我到底应该放什么,它到底会放在哪里?我需要包含全部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦在深巷 2024-11-15 11:50:13

Google 的 Android 文档指出:

异步任务由 3 个通用类型(称为 Params、Progress 和 Result)和 4 个步骤(称为 onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute)定义。

AsyncTask 的通用类型:

异步任务使用的三种类型如下:

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:

 private class MyTask extends AsyncTask<Void, Void, Void> { ... }

您可以进一步参考:http ://developer.android.com/reference/android/os/AsyncTask.html

或者您可以参考Sankar-Ganesh 的博客

典型的 AsyncTask 类的结构如下:

private class MyTask extends AsyncTask<X, Y, Z>

    protected void onPreExecute(){

    }

该方法在之前执行启动新线程。没有输入/输出值,因此只需初始化变量或您认为需要执行的任何操作。

    protected Z doInBackground(X...x){

    }

AsyncTask 类中最重要的方法。您必须将您想要在后台执行的所有操作放置在与主线程不同的线程中。这里我们有一个来自类型“X”的对象数组作为输入值(你在标题中看到了吗?我们有“...extends AsyncTask”这些是输入参数的类型)并从该类型返回一个对象“Z”。

   protected void onProgressUpdate(Y y){

   }

使用publishProgress(y)方法调用此方法,通常在您想要在主屏幕中显示任何进度或信息时使用它,例如显示您在后台执行的操作进度的进度条。

  protected void onPostExecute(Z z){

  }

该方法在后台操作完成后调用。作为输入参数,您将收到 doInBackground 方法的输出参数。

X、Y 和 Z 类型怎么样?

从上面的结构中可以推断出:

 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.

我们如何从外部类中调用这个任务?只需以下两行:

MyTask myTask = new MyTask();

myTask.execute(x);

其中x是X类型的输入参数。

一旦我们的任务运行,我们就可以从“外部”找到它的状态。使用“getStatus()”方法。

 myTask.getStatus();

我们可以收到以下状态:

RUNNING - 表示任务正在运行。

PENDING - 表示任务尚未执行。

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

有关使用 AsyncTask 的提示

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

  2. 您不能在另一个 AsyncTask 或线程内调用 AsyncTask。方法execute 的调用必须在UI 线程中完成。

  3. onPostExecute 方法在 UI Thread 中执行(这里可以调用另一个 AsyncTask!)。

  4. 任务的输入参数可以是一个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:

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.

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> { ... }

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 :

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){

    }

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){

   }

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){

  }

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:

 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);

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.

 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.

FINISHED - Indicates that onPostExecute(Z) has finished.

Hints about using AsyncTask

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

  2. You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread.

  3. The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!).

  4. The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.

自在安然 2024-11-15 11:50:13

我参加聚会已经太晚了,但我认为这可能会对某人有所帮助。

< /a>

I'm too late to the party but thought this might help someone.

谁对谁错谁最难过 2024-11-15 11:50:13

保持简单!

AsyncTask 是在后台线程中运行的后台任务。它接受输入,执行进度并给出输出

AsyncTask<输入、进度、输出>

在我看来,混乱的主要根源在于我们尝试记住 AsyncTask 中的参数。
关键是不要死记
如果您可以想象您的任务真正需要做什么,那么使用正确的签名编写 AsyncTask 将是小菜一碟。
只需弄清楚您的输入进度输出是什么,您就可以开始了。

例如:
输入图像描述这里

AsyncTask 的核心!

doInBackgound() 方法是 AsyncTask 中最重要的方法,因为

  • 只有该方法在后台线程中运行并将数据发布到 UI 线程。
  • 它的签名随 AsyncTask 参数而变化。

让我们看看其中的关系

在此处输入图像描述

doInBackground()onPostExecute()onProgressUpdate() 也相关

在此处输入图像描述

显示代码< /strong>
那么我该如何编写 DownloadTask 的代码呢?

DownloadTask extends AsyncTask<String,Integer,String>{

      @Override
      public void onPreExecute()
      {}

      @Override
      public String doInbackGround(String... params)
      {
               // Download code
               int downloadPerc = // calculate that
               publish(downloadPerc);

               return "Download Success";
      }

      @Override
      public void onPostExecute(String result)
      {
          super.onPostExecute(result);
      }

      @Override
      public void onProgressUpdate(Integer... params)
      {
             // show in spinner, access UI elements
      }

}

您将如何运行此任务

new DownLoadTask().execute("Paradise.mp3");

Keep it simple!

An AsyncTask is background task which runs in the background thread. It takes an Input, performs Progress and gives Output.

ie AsyncTask<Input,Progress,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:
enter image description here

Heart of the AsyncTask!

doInBackgound() method is the most important method in an AsyncTask because

  • Only this method runs in the background thread and publish data to UI thread.
  • Its signature changes with the AsyncTask parameters.

So lets see the relationship

enter image description here

doInBackground() and onPostExecute(),onProgressUpdate() are also related

enter image description here

Show me the code
So how will I write the code for DownloadTask?

DownloadTask extends AsyncTask<String,Integer,String>{

      @Override
      public void onPreExecute()
      {}

      @Override
      public String doInbackGround(String... params)
      {
               // Download code
               int downloadPerc = // calculate that
               publish(downloadPerc);

               return "Download Success";
      }

      @Override
      public void onPostExecute(String result)
      {
          super.onPostExecute(result);
      }

      @Override
      public void onProgressUpdate(Integer... params)
      {
             // show in spinner, access UI elements
      }

}

How will you run this Task

new DownLoadTask().execute("Paradise.mp3");
岁月无声 2024-11-15 11:50:13

请参阅以下链接:

  1. http://developer.android.com/reference/android /os/AsyncTask.html
  2. http://labs.makemachine .net/2010/05/android-asynctask-example/

您不能传递超过三个参数,如果您只想传递 1 个参数,则对其他两个参数使用 void 。

1. private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> 


2. protected class InitTask extends AsyncTask<Context, Integer, Integer>

异步任务由在后台线程上运行的计算定义,其结果在 UI 线程上发布。异步任务由 3 个通用类型(称为 Params、Progress 和 Result)和 4 个步骤(称为 onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute)定义。

Refer to following links:

  1. http://developer.android.com/reference/android/os/AsyncTask.html
  2. http://labs.makemachine.net/2010/05/android-asynctask-example/

You cannot pass more than three arguments, if you want to pass only 1 argument then use void for the other two arguments.

1. private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> 


2. protected class InitTask extends AsyncTask<Context, Integer, Integer>

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.

戏舞 2024-11-15 11:50:13
  • 简而言之,AsyncTask中有3个参数

    1. DoInBackground(String... params) 中使用的输入参数

    2. OnProgressUpdate(String... status) 中使用的显示进度状态的参数

    3. OnPostExcute(String... result) 中使用结果的参数

    注意: - [参数类型可以根据您的要求而变化]

  • in Short, There are 3 parameters in AsyncTask

    1. parameters for Input use in DoInBackground(String... params)

    2. parameters for show status of progress use in OnProgressUpdate(String... status)

    3. parameters for result use in OnPostExcute(String... result)

    Note : - [Type of parameters can vary depending on your requirement]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文