Android AsyncTask Example Tutorial

Filed Under: Android

Today we will look into Android AsyncTask. We will develop an Android example application that performs an abstract AsyncTask in background.

Android AsyncTask

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive.

Android application runs on a single thread when launched. Due to this single thread model tasks that take longer time to fetch the response can make the application non-responsive. To avoid this we use android AsyncTask to perform the heavy tasks in background on a dedicated thread and passing the results back to the UI thread. Hence use of AsyncTask in android application keeps the UI thread responsive at all times.

The basic methods used in an android AsyncTask class are defined below :

  • doInBackground() : This method contains the code which needs to be executed in background. In this method we can send results multiple times to the UI thread by publishProgress() method. To notify that the background processing has been completed we just need to use the return statements
  • onPreExecute() : This method contains the code which is executed before the background processing starts
  • onPostExecute() : This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method
  • onProgressUpdate() : This method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update the UI thread

The three generic types used in an android AsyncTask class are given below :

  • Params : The type of the parameters sent to the task upon execution
  • Progress : The type of the progress units published during the background computation
  • Result : The type of the result of the background computation

Android AsyncTask Example

To start an AsyncTask the following snippet must be present in the MainActivity class :

MyTask myTask = new MyTask();

In the above snippet we’ve used a sample classname that extends AsyncTask and execute method is used to start the background thread.


  • The AsyncTask instance must be created and invoked in the UI thread.
  • The methods overridden in the AsyncTask class should never be called. They’re called automatically
  • AsyncTask can be called only once. Executing it again will throw an exception

In this tutorial we’ll implement an AsyncTask that makes a process to go to sleep for a given period of time as set by the user.

Android Async Task Project Structure

android asynctask, android asynctask example

Android AsyncTask Example Code

The xml layout is defined in the activity_main.xml and its given below:


<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Sleep time in Seconds:"/>
        android:text="Run Async task"
        android:layout_marginTop="64dp" />
        android:layout_centerHorizontal="true" />

In the above layout we’ve used a predefined drawable as the border of the EditText.

The is defined below:

package com.journaldev.asynctask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private EditText time;
    private TextView finalResult;

    protected void onCreate(Bundle savedInstanceState) {
        time = (EditText) findViewById(;
        button = (Button) findViewById(;
        finalResult = (TextView) findViewById(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AsyncTaskRunner runner = new AsyncTaskRunner();
                String sleepTime = time.getText().toString();

    private class AsyncTaskRunner extends AsyncTask<String, String, String> {

        private String resp;
        ProgressDialog progressDialog;

        protected String doInBackground(String... params) {
            publishProgress("Sleeping..."); // Calls onProgressUpdate()
            try {
                int time = Integer.parseInt(params[0])*1000;

                resp = "Slept for " + params[0] + " seconds";
            } catch (InterruptedException e) {
                resp = e.getMessage();
            } catch (Exception e) {
                resp = e.getMessage();
            return resp;

        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation

        protected void onPreExecute() {
            progressDialog =,
                    "Wait for "+time.getText().toString()+ " seconds");

        protected void onProgressUpdate(String... text) {

In the above code we’ve used AsyncTaskRunner class to perform the AsyncTask operations. The time in seconds is passed as a parameter to the class and a ProgressDialog is displayed for the given amount of time.

The images given below are the outputs produced by the project where the time set by the user is 5 seconds.
android asynctask example, AsyncTask

This brings an end to the tutorial. You can download the final Android AsyncTask Project from the below link.


  1. Chibuzor Olumor says:

    Please sir how can I use Async Task to display an image or a progress bar while another activity loads?

  2. sirine says:

    i have problem :
    i have a class MainActivity and i create an other class (public class MySql extends AsyncTask)
    i have return boolean value
    and now i want get this value , so how ???

  3. Umair says:

    Very helpful to understand and implement AsyncTask abstract class as a Helper class.

  4. jaypal says: Cannot run program “su”: error=13, Permission denied

    I got this error when I used your code for updating an android app from website stored apk.

  5. Rakshan Premsagar Kapikad says:


    Is there any possibility of returning an object from Async Task and using the attributes of that object in my main activity?


    I have an Async Task with parameters(void,void,Uaclient)
    protected Uaclient doInBackground(){..return client}
    protected onPostExecute(Uaclient client){}

    how to access this client in Mainactivity? I get a NullPointer error

  6. SAJAN.V says:

    Your stuffs are great.God bless u

  7. Faizan says:

    Hello ,Goodevening Your stuff is best it helps me a lot but i just have one doubt plz give me a solution for it

    i have done image slider exmaple using viewflipper the image slides properly but when i touch it it does not stops it stilss keep on sliding what should i implement so that whenever i long press the image should stop sliding n when i lift my finger it gets started again please help me though this please thankyou

  8. Raja says:

    By using Asynctask connect from one activity into another activity(intent) example in android..

  9. Abbos says:

    I have a question. I have created a project. I must that two asyntask methods need work at same time on the project. But I can not do with asyntask. What can you advise me? Thanks for the answer.

    1. Sapna says:

      I want to bind the sqlite DB with the frot end using AsyncTask can please help me do this?
      SQLite DB is created and stored in the local database and now i have to call to front end UI and display. This is offline project. and data from JSON is stored in SQLite.

      Thank You!

  10. athira says:

    Superb article… I was on my final year mini project, I was stuck when NetworkonMainThread Exception came. I searched google docs asynctask but it makes me more confused. But you did really great. For beginners like me, these type of blog posts is needed.

    1. Anupam says:

      Glad it helped you Athira.

  11. b0uda says:

    Hi , thx for the tutorial and i have a question please.
    how can we access the time variable witch is a local variable of the try catch block of the doInBackground function!!!!!

    1. Me says:

      Good question!

      What is the answer?

    2. Hossein says:

      Hi b0uda,

      Obviously the integer “time” variable of the “try block” belongs to
      that block and we can not use it outside of this block of code…but on “onPreExecute()”
      method : you can see “time” again…but this is “private EditText time” , declared at the
      beginning of the MainActivity ,NOT the “time” variable of the “try block” .

  12. shivaji says:

    Very nice explanation . Great article !! thanks

  13. shihab says:

    Woow..Cool article..This article removed all of my confusion about AsyncTask..
    Thank you very much..

    1. gopinath says:

      really nice

  14. krishna kulat says:

    public String sendPostRequest(String requestURL,
    HashMap postDataParams) {

    URL url;

    StringBuilder sb = new StringBuilder();
    try {
    url = new URL(requestURL);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    OutputStream os = conn.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
    new OutputStreamWriter(os, “UTF-8”));

    int responseCode = conn.getResponseCode();

    if (responseCode == HttpsURLConnection.HTTP_OK) {
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    sb = new StringBuilder();
    String response;
    while ((response = br.readLine()) != null){

    } catch (Exception e) {
    return sb.toString();

    private String getPostDataString(HashMap params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry entry : params.entrySet()) {
    if (first)
    first = false;

    result.append(URLEncoder.encode(entry.getKey(), “UTF-8”));
    result.append(URLEncoder.encode(entry.getValue(), “UTF-8”));

    return result.toString();

  15. Tim Verdouw says:

    Thanks, that was so easy to implement and wow the app runs so much faster now. Thanks so much for your time to make this tutorial. Really appreciated.

  16. Champak says:

    Great explanation
    Another beautiful implementation of Async Task is creating animations like in this app

  17. Manish says:

    I am trying to replace pageradapter activity with AsyncTask IS IT POSSIBLE WITH YOUR TUTORIAL?

  18. Thanks for you great tutorials

    I have one question

    Is it good for using AsyncTask to download a file with the size up to 100MB?

  19. Ami charadava says:

    Good Explaination.

  20. Mr Khanh says:

    your post is very good. thank you for sharing.

  21. Samir says:

    Thanks very usefulll

  22. Abhishek Purohit says:

    Dear Sir,
    I have an unique problem. In my app I need to refresh displayed data almost every second, the data are stored on a SQL Server hosted on a web hosting provider, Now I’ve completed development and the results are promising just one big problem. I’m using Timer to run queries: like this
    protected void onResume() {
    //onResume we start our timer so it can start when the app comes from the background
    protected void onStop() {
    public void startTimer() {
    //set a new Timer
    timer = new Timer();

    //initialize the TimerTask’s job

    //schedule the timer,
    timer.schedule(timerTask, 0, 1500); //
    public void stopTimerTask() {
    //stop the timer, if it’s not already null
    if (timer != null) {
    timer = null;
    public void initializeTimerTask() {
    timerTask = new TimerTask() {
    public void run() {
    //use a handler to run a toast that shows the current timestamp Runnable() {
    public void run() {
    populateList(); //runs queries to the sql server and updates appropriate textviews


    Now this approach is working if I just watch the data, If I want to interact with the ui it freezes and slows down. In your example of AsyncTask you’ve mentioned working off UI thread can this solve my problem a simple code example will save my life. Please help if you can.

    1. abhishek says:

      Abhishek, you are not suppose to fetch the data from server using main thread. coz that will crash your app. You must need to use asyntask to fetch data.

  23. jorge says:

    thanks great work

  24. Kayun Chan says:

    Thank you for such a great tutorial. It is clear and understandable.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content