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 liked 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 it and stop it 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 a 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 other activity (in this case Im not talking about Android Activities, its just the common word “activity”), you go to 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 fomm a very 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 background.
Now that we have a more clear understanding of how a Service works, let's move deeper into the Service construction.
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 here, in the Manifest, if not, the application will not recognize it.
<service android:name=".subpackagename.ServiceName"/>
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 a 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 is executed the run method.
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 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.