Developing Android Applications Part 6: Services

Services are one of the major features of the Android application framework. They are a very simple concept in principle but it’s very easy to get confused about what a Service actually is. My recommendation is therefore to approach Services with a completely open mind – imagine they’re a completely new topic, and try to take in exactly what they are as well as what they can do.

What is a service?

A service can serve a number of purposes;

  • It can run a thread to keep something running in a loop / continuously
  • It can expose its functionality to other applications.
  • It can do other stuff, but just focus on the above for now M’kay!

The classic example you’ll see given everywhere, including in google’s introductory documentation, is a “music playing service”. You’ve already seen Activities, so you could logically conclude that a music playing application would have activities such as “choose a song” or “look at all of my albums”. When the application actually needs to play music though, your users will think it’s pretty useless if the tunes stop playing as soon as you stopped engaging in one of the aforementioned activities. You would expect the music to keep going if you started it regardless of what activity you were running. To take this one step further, you would also probably expect the music to keep playing if you returned to your home page! Therefore, activities can not perform the “playing music” role. In this case, a services would be used instead.

Services can do more than just play music though. Whilst that is a good example (and probably closer to what services will be more ‘generally’ used for) it skips over the other cool feature which is that a service can expose itself to other applications, allowing them to use its features. This functionality has more in common with a “web service”, and it’s this similarity that may very well be the inspiration behind the “service” naming convention. When you think of a web service, you think of an interface somebody has provided to you in order to easily accomplish a task which you would otherwise have had to program yourself (such as getting traffic reports or telling you the weather). Here, the exact same principle applies. You only need to use the tag in the application manifest to specify what it is your service needs access to and you’re away. Other uses you’re likely to see for services on actual Android devices include;

  • Services which check messages periodically
  • Services which check emails periodically
  • Live Wallpapers

Google themselves take the ingenious step of explaining what services are by describing what they aren’t. One can see how easy it would be to jump to the conclusion that a service is a separate process; it keeps running even when i’ve apparently “exited” my app, therefore it must have it’s own process right? Wrong! Your application itself is the process, and a service is nothing more than another component of your application. Here’s a demonstration to show you this principle in practice. Fire up an emulator (you can do this by running any example code you’ve made from one of the previous articles, or by running one of the samples that google provides). Incidentally, if you’re lucky enough to own an android phone, then you can do this exercise on the device itself too. Once the emulator has started up, get to the home page if you’re not already there (press the little house button on the emulator keypad) and touch the “launcher icon”, which is the little grid of squares that the robot dude is telling you to press in the following screenshot;

Once you’ve pressed that you should see a list of all applications installed on your real/virtual device. Fire up the “browser” application by clicking its icon. If you’re using the emulator, and unless you’ve messed with the defaults, your home page should be google (AS IT SHOULD BE!!!). Click the URL bar and browse to another site, in this case, I’ve gone to wikipedia;

Hit the home button again. Note that you return to the home page and your application appears to have closed. Press the “menu” button (again, from your emulators “physical keys” on the right). Incidentally, while we’re on the subject of those it’s worth pointing out that not all phones will have all keys, but all phones will have a means ‘to have done the same thing as pressing those keys’. Therefore, while there is a full set of a-z letters provided to you there, and indeed some phones do have a physical keyboard (HTC Desire Z, for example), you can’t rely on them being physical but you CAN rely on there being ‘another way to press them’ if they’re not. Anyway, moving on, after pressing “menu” select the “settings” option;

From the settings menu, select “Applications”;

and then finally select “Running Services”. You’l be presented with a screen like this;

Now, there are two services running on my device; one of them you’ll probably have too, but the other one (bouncing cat heads) is hopefully just exclusive to my device! Anyway, if you look at the screen you’ll see that there is a grey bar followed a list of things. The grey bar is the application (which in itself is a process), the stuff directly below the grey bar represents the services within that application. On the screen-shot there’s only one service running in each application, but you can have more than one. Notice the absence of the “browser” application though?

Now hit the “Home” physical key to return to the home page, then use the launcher icon to bring your browser back up. You’ll notice that the browser has returned to wikipedia (or whatever other website you decided to visit earlier on in the steps). The browser has remembered where you were, without using a service.

Hopefully this should be enough to convince you that services aren’t processes, applications are!

So I guess you’re wondering how long that application would keep running then? How long exactly would that browser remember that you were on the wikipedia page last time you used it? Well, that’s not really about services but I’ll answer it anyway: It’ll remain “remembered” as long as there is memory to do so. If memory starts running out, the “oldest remembered” app gets its state erased.

So what were the bouncing cat heads??!

Yeah, that other service that was running. Well, that’s actually the service example I’m going to be demonstrating next. It’s an example of a “live wallpaper” service, which are those funky moving wallpapers. Stay tuned if you want to know how to implement bouncing rotating cat heads on your android phone!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s