最終更新:2017-02-11 (土) 09:10:40 (2628d)  

android.os.AsyncTask
Top / android.os.AsyncTask

public abstract class android.os.AsyncTask<Params, Progress, Result>

AsyncTask enables proper and easy use of the UI thread.

http://developer.android.com/reference/android/os/AsyncTask.html

使い方

  • defined by 3 generic types, called Params, Progress and Result,
  • and 4 steps, called onPreExecute?, doInBackground?, onProgressUpdate? and onPostExecute?.

パラメータ

Params

Progress

Result

メモ

  • AsyncTaskはExecutorによってAsyncTaskを複数実行したときの挙動が決まる
  • API11以降ではTHREAD_POOL_EXECUTOR, SERIAL_EXECUTORが選べ、デフォルトはSERIAL_EXECUTOR

用途

  • 数秒の処理用
  • 特定の画面を見ている間に裏で処理をする

長い処理

親クラス

メソッド

  • AsyncTask.execute?

概要

  • You can specify the type of the parameters, the progress values, and the final value of the task, using generics
  • The method doInBackground() executes automatically on a worker thread onPreExecute?(), onPostExecute?(), and onProgressUpdate?() are all invoked on the UI thread
  • The value returned by doInBackground?() is sent to onPostExecute?()
  • You can call publishProgress() at anytime in doInBackground?() to execute onProgressUpdate?() on the UI thread
  • You can cancel the task at any time, from any thread

動作

  • AsyncTask.onPreExecute? - invoked on the UI thread before the task is executed.
  • AsyncTask.doInBackground - invoked on the background thread immediately after onPreExecute?() finishes executing.
  • AsyncTask.onProgressUpdate? - invoked on the UI thread after a call to publishProgress(Progress...).
  • AsyncTask.onPostExecute - invoked on the UI thread after the background computation finishes.

参考

関連