Using a Background Process for a Heavy Task in Our Android Application

Using a Background Process for a Heavy Task in Our Android Application
Page content

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();

webServiceTask.execute(WEB_SERVICE_URL,this);

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 = ProgressDialog.show(WebServiceBackgroundActivity.this, “”, “LoadingData. Wait…”, true);

dialog.show();

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 alertList){

AlertasAdapter ad = new AlertasAdapter(this, alertList);

this.setListAdapter(ad);

dialog.dismiss();

}

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) {

try{

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

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

callerActivity.populateListWithAlerts(alertas);

}catch(Exception e){

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

}

super.onPostExecute(response);

}

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

References

Android source code Project, https://github.com/jbeerdev/BrightHubCode

This post is part of the series: 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