Skip to main content

Android Background processing with AsyncTask

I had to download data from server and bind it with the ListView.I studied about this and finally used AsyncTask for background processing.I'm going to post some code and it's description about Android background processing.


AsyncTask is designed to perform background processing and publish result on the UI thread.It is a helper class around Thread and Handler and does not constitute a generic threading framework.AsyncTask is defined by three generic types called Params,Progress and Result.And it has four callback methods called onPreExecute(),onPostExecute(),doInBackground() and onProgressUpdate().

public Load extends AsyncTask<Params,Progress,Result>

AsyncTask's generic types

Params:-It's a parameters send to the task upon execution.
Progress:-This parameter shows progress units during background processing.
Result:-It return type of result of the background processing.
If you don't require it's type then you can just use Void instead of it's generic types. 

public Load extends AsyncTask<Void,Void,Void>

AsyncTask's Callback methods

onPreExecute():-It invoke on the UI thread before the task execution.Generally we show ProgressDialog here if we want to show progress Dialog till background processing is completed.
onPostExecute():-It invoke on the UI thread after the task execution.Generally we dismiss ProgressDialog here if we want to dismiss Dialog when background processing is completed.
doInBackground():-It invoke on the background thread immediately after onPreExecute() execute.We put here code that we have to execute on the background thread like image downloading etc.
onProgressUpdate():-It invoke on the UI thread. This method is used to display any form of progress in the user interface while the background processing is still executing.
 We can also cancel task by calling cancel(true).

Example of Android background processing with AsyncTask.

public class Load extends AsyncTask<Void, Void, Void> {
    Context context;
    ProgressDialog progressDialog;

    public LoadLatestLive(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        /*put here code that you have to execete in the background thread*/
        return null;

    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progressDialog =, "", "Loading");

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub


Popular posts from this blog

Display video from specific folder with rounded shape thumbnail

Hi friends ,This is complete post, about how to display video form specific folder and display it with rounded shape and play it on click event. For Rounded shape I have used custom ImageView.
Now have a look on code part.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android=""
    android:stretchMode="columnWidth" >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

Custom Android SeekBar Developement

Before starting coding for Custom SeekBar,You must have good concept of following topics. 1.Layer List 2.Animation List 3.Selector Layer List:A LayerDrawableis a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
<layer-listxmlns:android=""> <item android:id="@android:id/background" android:drawable="@drawable/seekbar_total"/> <itemandroid:id="@android:id/secondaryProgress"> <clipandroid:drawable="@drawable/seekbar_buffer"/> </item> <itemandroid:id="@android:id/progress"> <clipandroid:drawable="@drawable/seekbar_played"/> </item> </layer-list> Animation List:A AnimationDrawable is a drawable object that is used to create frame-by-frame animation.It's oneshotelement should be true if u want loop animation otherwise false.

Custom Volley-Gson request implementation

This page covers Volley-Gson integration and also Volley custom Request. We assume that, you are familiar with Volley and Gson.

Volley has some inbuilt request which you can use, if your response is a String, Image, or JSON, in that case, you don't need to implement a custom Request.  To use Gson with Volley, we need to implement a custom request.
To make a custom request, you need to perform following operation.
Extend Request<T> class, where T is the type of parsed response or expected response. Implement the abstract methods parseNetworkResponse() and deliverResponse().  parseNetworkResponse:  A response contains parse response of a given type i.e Gson.

    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
          , HttpHeaderParser.parseCharset(response.headers));
            if (BuildConfig.IS_DEBUG)
                Log.d(TAG, "Response :: " + …