Need and Vital Role of AsyncTask in Android

Hi,

It is similar to option of XMLHttpRequest AJAX request  in Javascript.




 Android implements a single thread  model and whenever an Android application is launched,
 a thread is created.

 Consider the following Scenario:

 When you have long running operations like a network call on a button click  in your application.

 On that button click a request would be made to the server and  response will be awaited. Now due to the single thread model of Android,  till the time response is awaited your UI screen hangs or in other words,
 it is non-responsive.

 We can overcome this by creating a new Thread and implement  the run method to perform the time consuming operation,  so that the UI remains responsive.

As shown below a new Thread is created in onClick method
view source print.

public void onClick(View v) {
Thread t = new Thread(){
public void run(){
// Long running operation
}
};
t.start();
}

 But since Android follows single thread model and Android UI toolkit is not thread safe, so if there is a need  to make some change to the UI based on the result of the operation performed, then this approach may lead some issues.

 There are various approaches via which control can be given back to UI thread (Main thread running the application).

Handler approach is one among the various approaches.


The code snippet which shows below helps us to understand Handler approach.
view source print?

public void onClick(View v) {
Thread t = new Thread(){
public void run(){
// Long time comsuming operation
Message myMessage=new Message();
Bundle resBundle = new Bundle();
resBundle.putString("status", "SUCCESS");
myMessage.obj=resBundle;
handler.sendMessage(myMessage);
}
};

t.start();
}

As seen I had modified the original run method and added code to create Message object, which is then passed as parameter to sendMessage method of Handler. Now let us look at the Handler. Note that the code below is  present in the main activity code.

view source print?

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Code to process the response and update UI.
}
};

 After the execution of long running operation, the result is set in Message and passed to sendMessage of
 handler. Handle will extract the  response from Message and will process and accordingly update the UI.  Since Handler is part of main activity, UI thread will be responsible for updating the UI.

 Handler approach works fine, but with increasing number of long operations, Thread needs to be created, run method needs to be implemented and Handler needs to be created. This can be a bit cumbersome. The Android  framework has identified this pattern and has nicely enveloped it into what is called an Android Async Task. Let  us look at how it can help simplify  things. Async Task

 Android Async Task takes cares of thread management and is the recommended mechanism for performing
 long running operations.

 Let us look at a sample class LongOperation, which extends the AsyncTask below: view source print?

private class LongOperation extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
// perform long running operation operation
return null;
}

/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
}

/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation.
//For example showing ProgessDialog
}

/* (non-Javadoc)
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Void... values) {
/* Things to be done while execution of long running operation
is in progress.
For example updating ProgessDialog */
}
}

Modify the onClick method as shown below:
view source
print?


public void onClick(View v) {
new LongOperation().execute("");
}

 As seen class LongOperation extends AsyncTask and implements 4 methods:

 1. doInBackground: Code performing long running operation goes in this  method. When onClick method isexecuted on click of button, it calls  execute method which accepts parameters and automatically calls
 doInBackground method with the parameters passed.

 2. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.

 3. onPreExecute: This method is called before doInBackground method is called.

4. onProgressUpdate: This method is invoked by calling publishProgress  anytime from doInBackground call this  method.

 Overriding onPostExecute, onPreExecute and onProgressUpdate is optional.

Points to remember:
 1. Instance of Async Task needs to be created in UI thread. As shown in  onClick method a new instance of
 LongOperation is created there. Also execute method with parameters should be called from UI thread.

2. Methods onPostExecute, onPreExecute and onProgressUpdate  should not be explicitly called.

 3. Task can be executed only once.

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...