Tutorial

Android AsyncTask Example Tutorial

Published on August 3, 2022
author

Anupam Chugh

Android AsyncTask Example Tutorial

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();
myTask.execute();

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

  • 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: activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:textColor="#444444"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="9dip"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="10dip"
        android:text="Sleep time in Seconds:"/>
    <EditText
        android:id="@+id/in_time"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_toRightOf="@id/tv_time"
        android:layout_alignTop="@id/tv_time"
        android:inputType="number"
        />
    <Button
        android:id="@+id/btn_run"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Run Async task"
        android:layout_below="@+id/in_time"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="7pt"
        android:layout_below="@+id/btn_run"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

In the above layout we’ve used a predefined drawable as the border of the EditText. The MainActivity.java is defined below:

package com.journaldev.asynctask;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        time = (EditText) findViewById(R.id.in_time);
        button = (Button) findViewById(R.id.btn_run);
        finalResult = (TextView) findViewById(R.id.tv_result);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AsyncTaskRunner runner = new AsyncTaskRunner();
                String sleepTime = time.getText().toString();
                runner.execute(sleepTime);
            }
        });
    }

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

        private String resp;
        ProgressDialog progressDialog;

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

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


        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            progressDialog.dismiss();
            finalResult.setText(result);
        }


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(MainActivity.this,
                    "ProgressDialog",
                    "Wait for "+time.getText().toString()+ " seconds");
        }


        @Override
        protected void onProgressUpdate(String... text) {
            finalResult.setText(text[0]);
            
        }
    }
}

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.

Download Android AsyncTask Example Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 10, 2016

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

- Kayun Chan

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 14, 2016

    thanks great work

    - jorge

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 1, 2016

      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 @Override protected void onResume() { super.onResume(); //onResume we start our timer so it can start when the app comes from the background startTimer(); } @Override protected void onStop() { super.onStop(); stopTimerTask(); } public void startTimer() { //set a new Timer timer = new Timer(); //initialize the TimerTask’s job initializeTimerTask(); //schedule the timer, timer.schedule(timerTask, 0, 1500); // } public void stopTimerTask() { //stop the timer, if it’s not already null if (timer != null) { timer.cancel(); timer = null; } } public void initializeTimerTask() { timerTask = new TimerTask() { public void run() { //use a handler to run a toast that shows the current timestamp handler.post(new 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. Thanks

      - Abhishek Purohit

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        October 19, 2016

        Thanks very usefulll

        - Samir

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          December 21, 2016

          your post is very good. thank you for sharing.

          - Mr Khanh

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 5, 2017

            Good Explaination.

            - Ami charadava

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              February 7, 2017

              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?

              - Android quiz

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 16, 2017

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

                - Manish

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  May 18, 2017

                  Great explanation Another beautiful implementation of Async Task is creating animations like in this app https://play.google.com/store/apps/details?id=developer.android.vd.diceroller

                  - Champak

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 23, 2017

                    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.

                    - Tim Verdouw

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.