Introducing AsyncTask
AsyncTask
was introduced in Android at API level 3, Cupcake, with the express purpose of helping developers to avoid blocking the main thread. The Async
part of the name of this class comes from the word asynchronous, which literally means not occurring at the same time.
AsyncTask
is an abstract class, and as such, must be subclassed for use. At the minimum, our subclass must provide an implementation for the abstract doInBackground
method, which defines the work that we want to get done off the main thread.
protected Result doInBackground(Params… params)
There are four other methods of AsyncTask
which we may choose to override:
protected void onPreExecute() protected void onProgressUpdate(Progress… values) protected void onPostExecute(Result result) protected void onCancelled(Result result)
Although we will override one or more of these five methods, we will not invoke them directly from our own code. These are callback methods, meaning that they will be invoked for us...