Thursday, November 3, 2011

What is an Async Task?


AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.

An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

1. Params, the type of the parameters sent to the task upon execution.
2. Progress, the type of the progress units published during the background computation.
3. Result, the type of the result of the background computation.    

public class BackgroundTask extends AsyncTask<Void(Parms),Integer(Progress),Bitmap(Result)> {
@Override
protected void onPreExecute() {
//For eg start your progress bar here
}
@Override protected Bitmap doInBackground(Void... params) {
// Web service call
}
@Override protected void onCancelled() {
//for the task to be cancelled
}
@Override protected void onProgressUpdate(Integer... values) {
//show logs in a text field.
}
@Override protected void onPostExecute(Bitmap img) {
//End your progress bar here
}
}

No comments:

Post a Comment