How to Use Services in Your Android Application via the Google Programming Environment

How to Use Services in Your Android Application via the Google Programming Environment
Page content

How Android Services Work

Sometimes, we would like to have an application running in the background of our device, waiting for any change in the system, or maybe we want to run an application with the usual Activities, and a “piece of code” running, in the background that checks in on the Internet, waiting for incoming SMS messages or other events.

If you’d like to start at the beginning, try this tutorial on developing Google Android Applications.

In those cases Android Services are available. They are classes without UI (Activities are classes with UI), so they can be executed in the background, in a separate thread and you can start and stop them from an Activity.

This could be difficult to understand so to make it more clear we’ll put up some examples.

Here’s the classic example of when a Service would be used: In an audio player. Imagine, you have an MP3 player (downloaded from Android Market, done by yourself or the default media player in the device), you can then select artist, albums, songs… you start playing a song, and then you want to go back to the home screen to start another activity (in this case I’m not talking about Android Activities, its just the common word “activity”), you go to the home screen, but the song you have selected is still playing…

A less classic example. You have a simple RSS reader, with a list that includes all new posts from a very well known blog. A Service will search for updates via the site, and then it will update the screen with the new info. All while running in the background.

Now that we have a more clear understanding of how a Service works, let’s move deeper into the Service construction.

Building a Service

First of all, we need to create the Service in the AndroidManifest.xml file. Remember, that every Activity, Service, Content Provider you create in the code, you need to create a reference for here, in the Manifest, if not, the application will not recognize it.

In the code, we need to create a class that extends from “Service”

public class ServiceName extends Service {

private Timer timer = new Timer();

protected void onCreate() {

super.onCreate();

startservice();

}

}

This is a way to create Services, there are others ways, or the way I use to work with them. Here, we create a Timer, that every X seconds, calls to a method. This is running until we stop it. This can be used, for example, to check updates in an RSS feed. The “Timer” class is used in the startservice method like this

private void startservice() {

timer.scheduleAtFixedRate( new TimerTask() {

public void run() {

//Do whatever you want to do every “INTERVAL”

}

}, 0, INTERVAL);

; }

Where INTERVAL, is the time, every time the run method is executed.

To stop the service, we can stop the timer, for example, when the application is destroyed (in onDestroy())

private void stopservice() {

if (timer != null){

timer.cancel();

}

}

So, this application will be running in the background… But…How do we bind it with Activities? Or how can we start a Service from an Activity? In the next lesson we will delver deeper into Services.

Short term background process

Sometimes we need to accomplish short background tasks. For this, I recommend using AsyncTasks. Here you will find an article that explains how to work with them:

How to work with AsyncTask in Android.