Android: Why, When and How to use AsyncTask with example


What is AsyncTask?
               AsyncTask is an abstract class provided by Android which helps us to use the UI thread properly. This class allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.
When to use AsyncTask?
            Android implements single thread model and whenever an android application is launched, a thread is created. Assuming we are doing network operation on a button click in our application. On button click a request would be made to the server and response will be awaited. Due to single thread model of android, till the time response is awaited our screen is non-responsive. So we should avoid performing long running operations on the UI thread. This includes file and network access.
            To overcome this we can create new thread and implement run method to perform this network call, so UI remains responsive.
            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.

So the Android framework has given a very good pattern which is enveloped into AsyncTask.

Note: AsyncTask should ideally be used for operations that take few seconds. Some tasks keep the thread running for long time so in that case it is recommended to use java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

AsyncTask has four steps:

  1. doInBackgroundCode 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. onPostExecuteThis method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
  3. onPreExecuteThis method is called before doInBackground method is called.
  4. onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.
The task can be cancelled by invoking cancel(boolean) method. This will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object) method is called instead of onPostExecute() after doInBackground() returns. 

How to use AyncTask? Example application

         In this sample application I just made the process to sleep for some period of time instead of doing network operation.( Just to explain the concept of AsyncTask. This is not a realtime application).
In the UI thread user enters a value (time in milli seconds) which will be passed to AsyncTaskRunner.
AsyncTaskRunner is a private class which extends AsyncTask.


When the user clicks on the Run Async Task button, we make the process to sleep for give period of time. At the same time we keep the UI thread responsive by showing the status to the user.




















After completing the operation, show the final result to the user.


Steps to create the application:

1. Create new android application project in eclipse with default settings.
2. Create the layout for the application.
3. Create the MainActivity and AsyncTaskRunner at com.example.asynctaskdemo.MainActivity.java
4. Run the application as given in the above screenshots.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
     <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:textColor="#444444"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="9dip"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="10dip"
        android:text="Sleep time in milliseconds:"/>
    <EditText
        android:id="@+id/et_time"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_toRightOf="@id/tv_time"
        android:layout_alignTop="@id/tv_time"
        android:inputType="text"
        />
    <Button
        android:id="@+id/btn_do_it"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dip"
        android:layout_marginLeft="260dip"
        android:layout_below="@id/et_time"
        android:text="Run Async task" />
     <TextView
        android:id="@+id/tv_result"
        android:layout_width="400dip"
        android:layout_height="100dip"
        android:textSize="7pt"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/btn_do_it"
        android:layout_marginRight="9dip"
        android:layout_marginTop="15dip"
        android:layout_marginLeft="260dip"
        android:textColor="#AA0000"
        android:text=""/>
</RelativeLayout>


MainActivity.java

package com.example.asynctaskdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * @author Prabu
 * AsyncTask exmple
 *
 */
public class MainActivity extends Activity {
 private Button button;
 private EditText time;
 private TextView finalResult;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  time = (EditText) findViewById(R.id.et_time);
  button = (Button) findViewById(R.id.btn_do_it);
  finalResult = (TextView) findViewById(R.id.tv_result);
  button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    AsyncTaskRunner runner = new AsyncTaskRunner();
    String sleepTime = time.getText().toString();
    runner.execute(sleepTime);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 /**
  * @author Prabu
  * Private class which runs the long operation. ( Sleeping for some time )
  */
 private class AsyncTaskRunner extends AsyncTask<String, String, String> {

  private String resp;

  @Override
  protected String doInBackground(String... params) {
   publishProgress("Sleeping..."); // Calls onProgressUpdate()
   try {
    // Do your long operations here and return the result
    int time = Integer.parseInt(params[0]);    
    // Sleeping for given time period
    Thread.sleep(time);
    resp = "Slept for " + time + " milliseconds";
   } catch (InterruptedException e) {
    e.printStackTrace();
    resp = e.getMessage();
   } catch (Exception e) {
    e.printStackTrace();
    resp = e.getMessage();
   }
   return resp;
  }

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

  /*
   * (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(String... text) {
   finalResult.setText(text[0]);
   // Things to be done while execution of long running operation is in
   // progress. For example updating ProgessDialog
  }
 }
}




           Do you like to add your own tutorials here? Here is an opportunity... Check this link ! !             


 



Reactions:

25 comments :

  1. A very Simple and informative tutorial.

    ReplyDelete
  2. Thank you so much for this simple and quick but informative tutorial! I'll try to apply this to my own project now. :)

    ReplyDelete
  3. Your post is very clear and focused on methods we need to create an async task. Thx for sharing

    ReplyDelete
  4. Much appreciated this was incredibly clear and helpful for me

    ReplyDelete
  5. good work i would to share with you a lot, i would like to ask whether you are a java geek too.
    Hillary

    ReplyDelete
    Replies
    1. Yes, for more info , write to us cte.opinion@gmail.com

      Delete
  6. The tutorial was very informative and would be a great source for developers. Async task helps for easy usage of UI interface on that this post would be a good guide

    ReplyDelete
  7. Thanks so much for your quick but informative tutorial. I get Async task from this.

    ReplyDelete
  8. Thanks so much for your quick but informative tutorial, i get much from this

    ReplyDelete
  9. gr8...thanks a lot!!

    ReplyDelete
  10. Great sample. Thanks!

    ReplyDelete
  11. u may provide indepthly about this topic helpfull

    ReplyDelete
  12. such a great description.... thanks a lot.

    ReplyDelete
  13. Thank you very much ! :D Better than a book !

    ReplyDelete
  14. Thank you very much ! :

    ReplyDelete
  15. Great tutorial...well explained

    ReplyDelete
  16. Simple and easy to understand..great explanation and example...Thank You!!

    ReplyDelete
  17. It is truly a great and helpful piece of nice information.I am satisfied that you simply shared this useful information with us

    ReplyDelete
  18. Great... short and simple/// :)

    ReplyDelete
  19. Sir, thank you very much for your toturial. i succesfully implmented an asyncTask following your instructions.

    Best regards

    ReplyDelete
  20. I am new in android I read the code but I am curious how the result will be displayed in tv_result

    ReplyDelete
  21. i have implemented this example, but it throws an exception .. org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@410ea9b8)
    any idea ..?

    ReplyDelete
  22. Technically good example and all that, but AsyncTask itself is completely useless for accomplishing anything on a real device..

    Try this: Set the sleep time to 2 seconds and rotate the device.
    The result is that the asych task gets unceremoniously thrown away with the activity and the re-created activity has no idea that such task ever existed. I can't remember if the task gets interrupted on impotently runs to completion in a limbo of objects waiting for garbage collection, but that hardly matters, since no-one can ever receive the results of the task anyway.

    ReplyDelete

Ads