Pin Me

Working with AsyncTask in Android

written by: Jbeerdev•edited by: Simon Hill•updated: 5/2/2011

Using Services in Android allows us to have an application running in the background. But, what if we want to do a simple task in the background? AsyncTask is the answer.

  • slide 1 of 4

    Our Android application can become more complex with time, connections with a server, sending and receiving data from the web, storing big amounts of data in the Android database while showing a progress bar, or a notification in the notification bar... How do we do this, without interrupting the UI Thread to show a notification while we are receiving data from a server? First of all, what's the “UI Thread"?

    If you are familiar with the “Thread" term, it's easy to understand. Android applications have a main thread where all is processed. It's like having “OneTask" that does everything. If we had only this UI Thread to work with, you would not be able to do some things like, storing 10,000 data rows in the application at the same time you are using it. The application would stick, until the process “store 10,000 data rows" was finished. But, this is not the case! In Android you can have multiple threads running at the same time in one application. The UI Thread, allows you to move through the screen, while, for example, a background task is receiving data from a server and storing it in the database.

    Hope the term “Thread" is clear enough to go on.

    Now, how can we do this? Having a task running in the background? There is (at least, in my knowledge) various ways of doing it.

    One way is the “Old-java-way", and it's using the class Thread, the Handlers and Runnables. But I have a better way to do it, and it's using AsyncTask class.

  • slide 2 of 4

    The AsyncTask class

    Let's see the structure of a typical AsyncTask class.

    private class myBrightHubTask 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<X,Y,Z>" These are the TYPES of the input parameters) and returns an object from the type “Z".

    protected void onProgressUpdate(Y y){


    This method is called using the method publishProgress(y) and it is usually used when you want to show any progress or information in the main screen, like a progress bar showing the progress of the operation you are doing in the background.

    protected void onPostExecute(Z z){


    This method is called after the operation in the background is done. As an input parameter you will receive the output parameter of the doInBackground method.

    What about the X, Y and Z types?

    As you can deduce from the above structure:

    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.

    How do we call this task from an outside class? Just with the following two lines:

    myBrightHubTask brightHubTask = new myBrightHubTask();


    Where x is the input parameter of the type X.

    Once we have our task running, we can find out its status from “outside". Using the “getStatus()" method.


    and we can receive the following status:

    RUNNING - Indicates that the task is running.

    PENDING - Indicates that the task has not been executed yet.

    FINISHED - Indicates that onPostExecute(Z) has finished.

  • slide 3 of 4


    Here are some hints about using AsyncTask

    • Do not call the methods onPreExecute, doInBackground and onPostExecute manually. This is automatically done by the system.
    • You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread.
    • The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!).
    • The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.
  • slide 4 of 4

    Source Code

    A full project source code can be found here:

    BrightHubCode Git Repository

    The AsyncTask class is here:


    And the Activity that calls the AsyncTask is here: