Android Programming :: What is AsyncTask and how to use it with example?

AsyncTask is a way to create threads in your application. The android application runs on a main UI thread and all events and user input and your added functions are added in that UI thread to execute. The need to use a thread comes when you need to do a lengthy job which will take more then 5 seconds to complete. Why 5 seconds because that is the thresh hold implemented by android system. If any particular task in your application takes more then 5 seconds to execute and makes your application not responsive android will throw an exception to user’s stating application is not responding and you will see a “Force Close” dialog shown to users. All application developers know that once Force Close is displayed the chances of application getting uninstalled are high so we need to avoid it.

In order to avoid the application is not responding error and exception always make sure you use a thread wherever the time taken to perform a task is more then 5 seconds.

What is AsyncTask?

AsyncTask is a class provided by android which makes it easy to use the UI thread and make more thread as and when needed. This is the easiest way to create threads because it allows you to do background operations and send results to the UI thread without having to manipulate threads and/or handlers. AsyncTask was designed as a helper class around Thread and Handler which means that it contains all functions of Thread and Handler class but does not have any functions of generic threading in it.

How to use AsyncTask?

When using AsyncTask you need to import the following java/android classes:

import android.os.AsyncTask;

AsyncTask is defined as follows:

public class saveWallpaper extends AsyncTask<String, Void, Void>
{

}

AsyncTask provides a function named onPreExecute() which us executed before the thread has started running. It is defined as follows:

public class saveWallpaper extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute()
{

// run your code here…
}

}

AsyncTask provides a function named onPostExecute() which is executed after the process in the thread has finished executing. It is defined as follows:

public class saveWallpaper extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute()
{

// run your code here…
}

@Override
protected void onPostExecute()
{

// run your code here…
}

}

AsyncTask provides a function named doInBackground which executes the actual thread and the code and process you want to execute in thread. It is defined as follows:

public class saveWallpaper extends AsyncTask<String, Void, Void>
{
@Override
protected void onPreExecute()
{

// run your code here…
}

@Override
protected Void doInBackground(String… params)
{
// run your code here…

}

@Override
protected void onPostExecute()
{

// run your code here…
}

}

The String… params is the way to pass in the parameters to the thread. You can have any data type as needed and pass it to the thread.

We will keep updating our Android Programming Tips section every 4 days, please subscribe to our feed and also share our articles on Facebook and Google Plus so other’s can benefit from our insight. Please feel free to send us your comments below, on how we can improve.

No comments yet.

Leave a Reply