Why Context can't be used in doInBackground() of AsyncTask

前端 未结 3 951
孤街浪徒
孤街浪徒 2021-01-13 12:11

I am just finding what is the fact behind not using Context inside doInBackground(). In-fact we can\'t update the UI inside doInBackground() di

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 13:02

    AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

    AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

    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.

    For more information about using tasks and threads, read the Processes and Threads developer guide.

提交回复
热议问题