AsyncTask in Android Expample

AsyncTask is an abstract class that provides several methods managing the interaction between the UI thread and the background thread. it’s implementation is by creating a sub class that extends AsyncTask and implementing the different protected methods it provides.
Let’s demonstrate how to user AsyncTask by creating a simple activity that has two buttons and a progress bar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="vertical">
 <ProgressBar android:id="@+id/progressBar"
  style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent"
  android:layout_height="wrap_content" android:indeterminate="false"
  android:max="10" android:padding="10dip">
 </ProgressBar>
 <Button android:id="@+id/button1" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="Start Progress">
 </Button>
 <Button android:id="@+id/button2" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="Cancel">
 </Button>
 <TextView android:id="@+id/output" android:layout_width="match_parent"
  android:layout_height="wrap_content" android:text="Replace" />
</LinearLayout>



Creating the AsyncTask sub class:

The first step in implementing AsyncTask is to create a sub class like this:
class LongOperation extends AsyncTask<Params, Progress, Result>{
}
The AsyncTask declaration has three Varargs parameters which are:
  1. Params: parameter info passed to be used by the AsyncTask.
  2. Progress: the type of progress that the task accomplishes.
  3. The result returned after the AsyncTask finishes.
These parameters are of type Varargs which provide the flexibility to pass dynamic sized arrays as parameters.
In our example our class will be like this:
class LongOperation extends AsyncTask<Integer, Integer, Void>{
}
The parameter and the progress are of type Integer and the result is Void as our tasks does not return anthing (returns null).
The second step is overriding the protected methods defined by the AsyncTask class that handle the execution life cycle of the AsyncTask.
We have five methods to implement which are:
  1. onPreExecute: the first method called in the AsyncTask, called on the UI thread.
  2. doInBackground: the method that executes the time consuming tasks and publish the task progress, executed in background thread.
  3. onProgressUpdate: method that updates the progress of the AsyncTask, run on the UI thread.
  4. onPostExecute: the final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask.
  5. onCancelled: gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask.

Starting the AsyncTask:

To start the AsyncTask we create an instance of it, then call the execute() method passing the initial parameters like this:
LongOperation task=new LongOperation();
// start progress bar with initial progress 10
task.execute(10);

Implementing the AsyncTask:

 private class LongOperation extends AsyncTask<Integer, Integer, Void> {
        
   
     @Override
  protected void onPreExecute() {
   // initialize the progress bar
   // set maximum progress to 100.
   progress.setMax(100);
   text.setText("onPreExecute");
  }
  @Override
  protected void onCancelled() {
   // stop the progress
   progress.setMax(0);
   text.setText("start progress");
  }

  @Override
  protected Void doInBackground(Integer... params) {
   // TODO Auto-generated method stub
   // get the initial starting value
   int start=params[0];
   // increment the progress
   for(int i=start;i<=100;i+=5){
    try {
     boolean cancelled=isCancelled();
     //if async task is not cancelled, update the progress
     if(!cancelled){
      publishProgress(i);
      SystemClock.sleep(1000);
     }
    } catch (Exception e) {
     Log.e("Error", e.toString());
    }
   }
   return null;
  }
  @Override
  protected void onProgressUpdate(Integer... values) {
   // increment progress bar by progress value
   progress.setProgress(values[0]);
  }
  @Override
  protected void onPostExecute(Void result) {
   // async task finished
   Log.v("Progress", "Finished");
   text.setText("");
   text.append("finished");
  }

    }
Here are the steps:
  1. onPreExecute() method first gets called initializing the maximum value of the progress bar.
  2. doInBackground(Integer… params) methods gets called by obtaining the initial start value of the progress bar then incrementing the value of the progress bar every second and publishing the progress as long as the async task is not cancelled.
  3. onProgressUpdate(Integer… values) method is called each time progress is published from doInBackground, thus incrementing the progress bar.
  4. onPostExecute(Void result) is called after doInBackground finished execution.
  5. void onCancelled() is called if task.cancel(true) is called from the UI thread. it may interrupt the execution preventing onPostExecute from being executed.
The onClick handler of our buttons is like this:
@Override
 public void onClick(View v){ 
        //detect the view that was "clicked" 
     LongOperation task=new LongOperation();
  switch(v.getId()){
  case R.id.button1:
   task.execute(10);
   break;
  case R.id.button2:
   task.cancel(true);  
   break;
  }

 

The Difference between Handler and AsyncTask:

After we saw both Handlers and AsyncTasks a question may evolve: what’s the difference between the two and when to use one of them over the other ?
The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread.
AsyncTask provides a simple method to handle background threads in order to update the UI without blocking it by time consuming operations.
The answer is that both can be used to update the UI from background threads, the difference would be in your execution scenario. You may consider using handler it you want to post delayed messages or send messages to the MessageQueue in a specific order.
You may consider using AsyncTask if you want to exchange parameters (thus updating UI) between the app main thread and background thread in an easy convinient way.

Full code:
AsyncTaskDemo .java
package com.madhu.async;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AsyncTaskDemo extends Activity implements OnClickListener {
    Button btn,btn1;
    ProgressBar progress;
    TextView text;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn1 = (Button) findViewById(R.id.button2);
        text=(TextView)findViewById(R.id.output);
        progress=(ProgressBar)findViewById(R.id.progressBar);
                    //because we implement OnClickListener we only have to pass "this" (much easier)
        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
    }

    public void onClick(View v){
        //detect the view that was "clicked"
                LongOperation task=new LongOperation();
                                switch(v.getId()){
                                case R.id.button1:
                                                task.execute(10);
                                                break;
                                case R.id.button2:
                                                task.cancel(true);                           
                                                break;
                                }



    }

    private class LongOperation extends AsyncTask<Integer, Integer, Void> {
        
               
                @Override
                                protected void onPreExecute() {
                                                // initialize the progress bar
                                                // set maximum progress to 100.
                                                progress.setMax(100);
                                                text.setText("onPreExecute");
                                }

                                @Override
                                protected void onCancelled() {
                                                // stop the progress
                                                progress.setMax(0);
                                                text.setText("start progress");
                                }


                                @Override
                                protected Void doInBackground(Integer... params) {
                                                // TODO Auto-generated method stub
                                                // get the initial starting value
                                                int start=params[0];
                                                // increment the progress
                                                for(int i=start;i<=100;i+=5){
                                                                try {
                                                                                boolean cancelled=isCancelled();
                                                                                //if async task is not cancelled, update the progress
                                                                                if(!cancelled){
                                                                                                publishProgress(i);
                                                                                                SystemClock.sleep(1000);

                                                                                }

                                                                } catch (Exception e) {
                                                                                Log.e("Error", e.toString());
                                                                }

                                                }
                                                return null;

                                }
                                @Override
                                protected void onProgressUpdate(Integer... values) {
                                                // increment progress bar by progress value
                                                progress.setProgress(values[0]);

                                }

                                @Override
                                protected void onPostExecute(Void result) {
                                                // async task finished
                                                Log.v("Progress", "Finished");
                                                text.setText("");
                                                text.append("finished");
                                }


    }
}
Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.hcl.async"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AsyncTaskDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>



Comments

  1. Good example with great explanation...

    ReplyDelete
  2. Thanks ......let me know is there any changes in this example

    ReplyDelete

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams