什么参数传递给AsyncTask <arg1,arg2,arg3>?

mer*_*ort 153 android arguments android-asynctask

我不明白我应该放在这里以及这些论点最终的位置?究竟应该放什么,它究竟会去哪里?我是否需要包括所有3或者我可以包括1,2,20?

Kar*_*iya 485

谷歌的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.
Run Code Online (Sandbox Code Playgroud)

并非所有类型都始终由异步任务使用.要将类型标记为未使用,只需使用类型Void:

 private class MyTask extends AsyncTask<Void, Void, Void> { ... }
Run Code Online (Sandbox Code Playgroud)

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

或者您可以通过引用Sankar-Ganesh的博客来清楚AsyncTask的作用

那么典型的AsyncTask类的结构如下:

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

    protected void onPreExecute(){

    }
Run Code Online (Sandbox Code Playgroud)

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

    protected Z doInBackground(X...x){

    }
Run Code Online (Sandbox Code Playgroud)

AsyncTask类中最重要的方法.你必须在这里放置你想要在后台做的所有东西,与主要的不同.这里我们有一个输入值来自"X"类型的对象数组(你在标题中看到了吗?我们有"...... extends AsyncTask"这些是输入参数的类型)并从类型中返回一个对象"Z".

   protected void onProgressUpdate(Y y){

   }
Run Code Online (Sandbox Code Playgroud)

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

  protected void onPostExecute(Z z){

  }
Run Code Online (Sandbox Code Playgroud)

在后台操作完成后调用此方法.作为输入参数,您将收到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.
Run Code Online (Sandbox Code Playgroud)

我们如何从外部课堂调用此任务?只需以下两行:

MyTask myTask = new MyTask();

myTask.execute(x);
Run Code Online (Sandbox Code Playgroud)

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

一旦我们完成任务,我们就可以从"外部"找到它的状态.使用"getStatus()"方法.

 myTask.getStatus();
Run Code Online (Sandbox Code Playgroud)

我们可以获得以下状态:

RUNNING - 表示任务正在运行.

PENDING - 表示任务尚未执行.

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

关于使用AsyncTask的提示

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

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

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

  4. 任务的输入参数可以是Object数组,这样您就可以放置所需的任何对象和类型.

  • 比谷歌好. (27认同)
  • +1.SO成员,请注意.这就是你解释的方式.理解它虽然冗长,但却非常容易理解.谢谢Kartik. (24认同)
  • 完美的解释伙计 (17认同)
  • 很好的解释,现在我非常清楚Asyntask.:)谢谢Kartik (2认同)
  • 这为我清除了很多东西,很好的答案! (2认同)

mri*_*rid 68

我迟到了,但认为这可能对某人有所帮助.

  • 这种视觉效果非常有用.我将开始使用这样的更多视觉效果来展示当代码流不简单或技术或模式不常见时,类型及其相关变量如何捆绑在一起.我希望其他人能做更多的事情.谢谢 (3认同)
  • 有一天,我将其框起来并放在墙上,这样我就不必继续回到此主题以供参考。 (3认同)

Roh*_*ngh 11

把事情简单化!

一个AsyncTask是运行在后台线程后台任务。它接受一个Input,执行Progress并给出Output

AsyncTask<Input,Progress,Output>

在我看来,当我们试图记住表格中的参数时,造成混乱的主要原因是AsyncTask
关键是不要记住
如果您可以可视化任务真正需要执行的操作,那么AsyncTask使用正确的签名编写便是小菜一碟。
只需弄清楚您的输入进度输出是什么,您就可以轻松进行了。

例如: 在此处输入图片说明

AsyncTask的心脏!

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

  • 仅此方法在后台线程中运行,并将数据发布到UI线程。
  • 其签名随AsyncTask参数而变化。

所以让我们看一下关系

在此处输入图片说明

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

在此处输入图片说明

向我显示代码
那么如何为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
      }

}
Run Code Online (Sandbox Code Playgroud)

您将如何运行此任务

new DownLoadTask().execute("Paradise.mp3");
Run Code Online (Sandbox Code Playgroud)


Ket*_*mar 5

请参考以下链接:

  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>
Run Code Online (Sandbox Code Playgroud)

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

KPBird