Previously, we learn Transmitting Network Data Using Volley in Android. Now we see how to use AsyncTask  in Android.

Asynctask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

This article describes the usage of Asynctask in your application. It also covers how to handle the application lifecycle together with threads.

What is AsyncTask?

AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. Asynctask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.

When to use AsyncTask?

Assume you have created a simple Android application which fetch lot of data from database then you have need to use AsyncTask.

When an asynchronous task is executed from UI main thread, the task goes through 4 steps:


Invoked before the task is executed ideally before doInBackground method is called on the UI thread. This method is normally used to setup the task like showing progress bar in the UI.


Code running for long lasting time should be put in doInBackground method. When execute method is called in UI main thread, this method is called with the parameters passed.


Invoked by calling publish Progress at any time from doInBackground. This method can be used to display any form of progress in the user interface.


Invoked after background computation in doInBackground method completes processing. Result of the doInBackground is passed to this method.

Here I am creating an Android sample which help to clear concepts if Asynctask in Android.

1.      Create an Android project

2.      My activity_main.xml code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""



    android:orientation="vertical" >






        android:text="Load Data" >






        android:layout_height="fill_parent" />




 3.      Add following code in MainActivity.class

package com.example.androidAsyncTask



import android.content.Context;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebView;

import android.widget.Button;


public class MainActivity extends Activity {

            final Context context = this;


            /** Called when the activity is first created. */

            private ProgressDialog pDialog;

            Button myButton;



            public void onCreate(Bundle savedInstanceState) {




                        myButton = (Button) findViewById(;


                        pDialog = new ProgressDialog(this);

                        pDialog.setMessage("Loading data Please Wait....");


                        myButton.setOnClickListener(new OnClickListener() {



                                    public void onClick(View v) {

                                                // TODO Auto-generated method stub

                                                // Call Asyntask class

                                                LoadWebPageASYNC task = new LoadWebPageASYNC();

                                                task.execute(new String[] { "" });







            private class LoadWebPageASYNC extends AsyncTask<String, Void, String> {


                        protected void onPreExecute() {

                                    // Write your code which run first






                        protected String doInBackground(String... urls) {

                                    // write your code which you perform task in background


                                    // WebViewis a view that displays web pages

                                    WebView webView = (WebView) findViewById(;


                                    // load url


                                    // perform task which take time to complete. Here I am using for

                                    // loop

                                    for (int i = 0; i < 199999999; i++) {

                                                int a = 9;

                                                int b = 8;

                                                a = b;

                                                b = a;




                                    return "";




                        protected void onPostExecute(String result) {

                                    // write your code which you perform task in after doInBackground()






 Run your application


  Modified On Nov-30-2017 12:51:20 AM

Leave Comment