Pin Me

Android Development: Background Process for a Heavy Task

written by: Jbeerdev•edited by: Simon Hill•updated: 5/17/2011

This is the third part of the series of articles on accessing the web with your Android application and retrieving data. This article will focus on the right element you have to use in your Android application when you are 'doing a heavy task' like accessing the Internet.

  • slide 1 of 2

    Using AsyncTask?

    Android recommends using a background process to do a heavy task. For me a heavy task is one that takes more than a second to be completed. Why do I consider this a heavy task? Imagine you are using an Android application, this application has no background process and it connects to the Internet to retrieve some data, RSS feeds, for example. If the application does the Internet connection in the main UI thread, the application will get stuck until the task is finished. How much time will you wait for until you get desperate and turn off the phone? We expect the application to respond with fluency. If the application gets stuck very often while it's retrieving data from the web, it can be annoying for the user.

    That's why we use a background process for a heavy task. In the source code example I have done it in both ways, using AsyncTask and not using it. If you test the application in an Emulator or on a real device, you will see the difference.

    Now I will show you my solution to the problem using AsyncTask (full code avaliable).

    I have created an Activity:

    WebServiceBackgroundActivity extends ListActivity

    A ListActivity because I will show the elements retrieved from the Internet as a List.

    In the onCreate method, I call the AsyncTask:

    WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();


    I pass the AsyncTask the URL of the service I want to access and the Activity itself.

    If the onCreate I initialize a Spinner too:

    dialog =, "", "LoadingData. Wait...", true);;

    I have used a full screen spinner, but little spinners in the top of the screen can be used too. This is just just a way to tell the user that the application is doing something.

    I have created another method:

    public void populateListWithAlerts(List<Alertas> alertList){

    AlertasAdapter ad = new AlertasAdapter(this, alertList);




    This method will be called from the AsyncTask once is the task is finished, the method fills the List with the “Alertas" object. It dismisses the dialog too.

    The AsyncTask is implemented as follows:

    WebServiceAsyncTask extends AsyncTask<Object, Boolean, String>

    protected String doInBackground(Object... params) {

    String serviceUrl = (String) params[0];

    callerActivity = (WebServiceBackgroundActivity) params[1];

    BasicWebService webService = new BasicWebService(serviceUrl);

    return webService.webGet();


    When we called the AsyncTask, we passed to it 2 parameters, the first one is the serviceURL (String serviceUrl = (String) params[0]) and the second one the Activity that called the AsyncTask (callerActivity = (WebServiceBackgroundActivity) params[1]).

    Now we call the BasicWebService class and in the postExecute we do all the Gson conversion (this can be done in the doInBackground method too, but I have placed it in the postExecute to show more code about the AsyncTask), and we call the populateListWithAlerts method (from the caller Activity)

    protected void onPostExecute(String response) {


    Type collectionType = new TypeToken<List<Alertas>>(){}.getType();

    List<Alertas> alertas = new Gson().fromJson(response, collectionType);


    }catch(Exception e){

    Log.d("Error: ", " "+e.getMessage());




    If you want to know more about AsyncTask you can check the article How to use AsyncTask in Android.

  • slide 2 of 2


    Android source code Project,

Android Code to Access the Web

With this series of articles I will show Android developers a code example to connect your Android device to the Internet, retrieve data from it and treat that information. Using the right background process in Android is explained too.
  1. Android App Development: How to Access the Internet and Retrieve Info with Your App
  2. Android Development: GSON library and JSON Strings
  3. Android Development: Background Process for a Heavy Task