What arguments are passed into AsyncTask ? Ask Question

What arguments are passed into AsyncTask ? Ask Question

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?

ベストアンサー1

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

   }

このメソッドは、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.

このタスクを外部のクラスから呼び出すにはどうすればよいでしょうか? 次の 2 行だけで済みます。

MyTask myTask = new MyTask();

myTask.execute(x);

ここで、x はタイプ X の入力パラメータです。

タスクを実行すると、「getStatus()」メソッドを使用して、「外部」からタスクのステータスを確認できます。

 myTask.getStatus();

次のステータスを受け取ることができます。

ランニング- タスクが実行中であることを示します。

保留中- タスクがまだ実行されていないことを示します。

終了した- onPostExecute(Z)が終了したことを示します。

AsyncTask の使用に関するヒント

  1. onPreExecute、doInBackground、onPostExecute メソッドを手動で呼び出さないでください。これはシステムによって自動的に実行されます。

  2. AsyncTask を別の AsyncTask またはスレッド内で呼び出すことはできません。メソッド execute の呼び出しは UI スレッド内で実行する必要があります。

  3. onPostExecute メソッドは UI スレッドで実行されます (ここで別の AsyncTask を呼び出すことができます)。

  4. タスクの入力パラメータはオブジェクト配列にすることができます。これにより、必要なオブジェクトとタイプを自由に配置できます。

おすすめ記事