Pages

Get it free, Try now

Free Cell Phones

Wednesday, September 22, 2010

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,

AsyncTask
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
@Override
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 );
}

return "TASKCOMPLETED";
}

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

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

//showResults();
//progressDialog.dismiss();

super.onPostExecute(result);

}
// Called before doInBackground()
@Override
protected void onPreExecute() {
super.onPreExecute();
// do any initialization
}

// update the UI on complete of longrunningtask()
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}

}





Next article will follow on Services in Android.

3 comments:

  1. Waiting for article "Services" on Android.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. Right here, something like this

    doInBackground(){
    .....
    // getvaluefrmjni is param you should decide in wrapper which talks to C code
    longrunningtask(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.

    .....
    }

    ReplyDelete