Home > Android Dev > Using AsyncTasks in Android

Using AsyncTasks in Android

AsyncTasks, why do we need it?

As we are aware that any User interaction on any Android application should and must response with in 5 seconds, failure to which results in ANR Dialog. Yes “Application Not Responding” Dialog with ‘Wait’ and ‘Force Close’ inputs. Asynchronous operations can also be performed using Threads in accordance with Handler and messages to update UI about the action progress.

Here we will discuss code snippets on how AsyncTask can be incorporated to perform Synchronous operations updated the progress of operation.

Three types are used by AsyncTask, in this case datatype is a String,

First param String, this type of the parameters sent to the task upon execution.
Second param , this type of the Strings contains progress units published during the background operation.
Third param Result, this strings contains the result of the background operation.

Note* when no params are required use type void.

A class extend AsyncTask which triggers/perform long running tasks along side in override methods of AsyncTasks.
such as in our case
1. doInBackground(…)
2. onPreExecute()
3. rogressUpdate(…)
4. onCancelled()
5. onPostExecute(…)
Note* ‘…’ denotes function parameters

when myClass issues call to execute(…)

myClass.execute(“String”); //where “String” is the first param of the AsyncTask function call.

myClass.cancel(true);// onCancelled() is called.

below we will see the intention of each function call

protected class myClass extends AsyncTask

// Note that first param in the class definition matches the param passed to this method
// and the last param in the class definition matches the return type of this method
protected String doInBackground( String... params )
//-- on every iteration
//-- runs a while loop that causes the thread to sleep for 50 milliseconds
//-- publishes the progress - calls the onProgressUpdate handler defined below
//-- and increments the counter variable i by one
for(int i=0; i<=n; i++)
// longrunningtask();
publishProgress( i );


//when user issues a cancel on the operation.
protected void onCancelled() {
// Do a cleanup on operation canceled
// ReInitialise Data

// on successful completion of onPostExecute method is called
protected void onPostExecute(String result) {



// Called before doInBackground()
protected void onPreExecute() {
// do any initialization

// update the UI on complete of longrunningtask()
protected void onProgressUpdate(String... values) {


Next article will follow on Services in Android.

Categories: Android Dev
  1. September 27, 2010 at 2:26 pm

    Waiting for article "Services" on Android.

  2. December 17, 2010 at 5:15 pm

    I've been searching for a long but I couldn't find a solution for the next question. How can I call publish progress from a C function in JNI inside an AsyncTask? Thanks in advance.

  3. December 17, 2010 at 7:46 pm

    Right here, something like thisdoInBackground(){…..// getvaluefrmjni is param you should decide in wrapper which talks to C codelongrunningtask(getvaluefrmjni);// same param, pass it to publishprogress(),publishProgress(getvaluefrmjni ); Make sure paramters what you decide in wrapper shall be the same input value or one of the parameter of Asyctask……}

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: