Free Shipping

Secure Payment

easy returns

24/7 support

Use of AsyncTask in Android

 July 20  | 0 Comments

AsyncTask :

AsyncTask is an abstact class provided by Android which helps us to use the UI thread properly. This class around background operations and show the results on the UI without manipulate thread.

Get Skilled in Android Development


Android implements input tasks with a single user interface thread and this thread is called Main Thread. Main Thread cannot handle concurrent operations because it can handle only one operation at a time.

If we are doing network operation on a button click our application. On button click a request would be made to the server and take a response. So we should avoid performing long running operations on the UI thread.

AsyncTask has four steps :

1. doInBackgound()

2. onPostExecute()

3. onPreExecute()

4. onProcessUpdate()

doInBackground() : When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls in doInBackground().

onPostexecute() : This method is called after doInBackground() completes processing.

onPreExecute() : This method is called before doInBackground() is called.

onProgressUpdate() : This method is invoked by calling publishProgress anytime from this method call onProgressUpdate().

When to Use?

Assume yo uhave created a simple android app which downloads mp3 from internet .

Implementation :

Create a new class inside your Activity class and subclass it bye extending AyncTask as below:

private class DownloadMp3 extends AsyncTask<URL, Integer, Long> {

protected Long doInBackground(URL…urls) {


protected void onPostExecute(Long result) {


protected void onProgressUpdate(INTEGER…progress) {


Android Programming

Execute the task simply like below :

new DownloadMp3().execute(mp3URL);

You can download Example from below Link :

Keep visiting our site for more updates on Android and other technologies.