Advertisement
Tech

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

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.

By Jbeerdev
Desk Tech
Reading time 3 min read
Word count 598
Google android Mobile Android sdk
Using a Background Process for a Heavy Task in Our Android Application
Advertisement
Quick Take

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.

On this page

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.

Advertisement

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

I have created an Activity:

Advertisement

WebServiceBackgroundActivity extends ListActivity

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

Advertisement

In the onCreate method, I call the AsyncTask:

WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();

Advertisement

webServiceTask.execute(WEB_SERVICE_URL,this);

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

Advertisement

If the onCreate I initialize a Spinner too:

dialog = ProgressDialog.show(WebServiceBackgroundActivity.this, “”, “LoadingData. Wait…”, true);

Advertisement

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.

Advertisement

I have created another method:

public void populateListWithAlerts(List alertList){

Advertisement

AlertasAdapter ad = new AlertasAdapter(this, alertList);

this.setListAdapter(ad);

Advertisement

dialog.dismiss();

}

Advertisement

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:

Advertisement

WebServiceAsyncTask extends AsyncTask<Object, Boolean, String>

protected String doInBackground(Object… params) {

Advertisement

String serviceUrl = (String) params[0];

callerActivity = (WebServiceBackgroundActivity) params[1];

Advertisement

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
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Android sdk
Advertisement