Tuesday, 24 April 2012

Android Async Task

This blog post introduces one of the important mechanism of Async Task for Android with sample code.
Android implements single thread model and whenever an Android application is launched, a thread is created. Now assume you have long running operations like a network call on a button click in your application. On 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
?
1
2
3
4
5
6
7
8
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.

Handler

Let us look at the code snippet below to understand Handler approach.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
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 we have 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.
?
1
2
3
4
5
6
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:
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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:
?
1
2
3
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 is executed 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.
Hope this blog helped you understand Android async task and why it is important in Android application development.

No comments:

Post a Comment