How does AsyncTask work in Android

The main thread of an app should focus on the interface. All longer activities such as file, database or network access should be placed in the background. Android offers several techniques for this.


The class AsyncTask makes it possible to put a process in the background.

You create your own AsyncTask by expanding AsyncTask. It receives three generics parameters (also called type parameters) that are used for

  1. Input parameters
  2. Progress Message Values
  3. Return value
be set. import android.os.AsyncTask; public class MyAsyncTask extends AsyncTask You can then override the following methods:
  • doInBackground
    This method contains the code that should run in the background.
  • onPreExecute
    From here the surface can be edited before doInBackground is called.
  • onProgressUpdate
    Intermediate updates of the surface such as a progress bar can be updated from here.
  • onPostExecute
    The method is called after doInBackground has expired and receives the return value from doInBackground as a parameter. The method is synchronized with the surface thread. The results can therefore be displayed in the interface elements.
An instance of your own AsyncTask can be called using its execute method. It receives the parameter that was specified as the first type parameter (generics) when defining the AsyncTask class. An instance can only be called once via execute. MyAsyncTask async = new MyAsyncTask (); String param [] = {""}; async.execute (param); Each object of an AsyncTask can only be used once via execute be called. If a second execution is required, a new object must be created, otherwise an exception threatens.

Return the results

Often you want to use the results of the background jobs. For this purpose, you can, for example, store a callback that is called after the task has ended.

The self-defined class HttpAsyncTask, which extends AsyncTask, serves as an example. It should call the interested party via a self-defined interface HttpAsyncAntwort. In this case it is the activity itself.

public class HttpAsyncTask extends AsyncTask The reference to the response interface is passed to the constructor so that the object to be called can be saved. The interface is implemented as follows: public interface HttpAsyncAntwort {public void eventAntwort (String str); } The callback is in the method onPostExecute realized. public class HttpAsyncTask extends AsyncTask The AsyncTask is generated and called from the MainActivity. public class MainActivity extends AppCompatActivity implements HttpAsyncAntwort {TextView tv; // ... HttpAsyncTask asyncTask = new HttpAsyncTask (this); String url = // ... asyncTask.execute (url); // ... @Override public void event response (String str) {if (tv! = Null) {tv.setText (str); }}



private HandlerThread handlerThread; private trader handler; @Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); handlerThread = new HandlerThread ("MyHandlerThread"); handlerThread.start (); handler = new Handler (handlerThread.getLooper ()); } @Override public void onDestroy () {super.onDestroy (); handlerThread.quitSafely (); } The HandlerThread is started with the following lines from the main thread. (new Runnable () {public void run () {// it all works in the background}});

Access the UI thread

If a background thread wants to access the surface again, it can be a Runnable about the method runOnUiThread start with access to the UI thread, which can then access the elements of the activity. runOnUiThread (new Runnable () {@Override public void run () {textView.setText (answer);}});

Feedback via observer

Using an observer, an activity can register with a background thread as interested in changes. In this way, an activity can react to the end of a task without having to wait for it.