Bound Services. A way to interact with the Service

andoid bound services tutorial icon

Our tutorial app uses a Service to play a sound clip.

We start a Service when the app starts. The Service will run as long as the app is alive.

We need to bind to the Service to start and stop playing a sound clip. We can leave the clip playing in the Service while it is running. It will continue to play even if the activity is paused or destroyed.

Bound Services: a primer

Android bound services tutorial icon

Quick recap on Services

  • Services are app components that you can use to do work in the background.
  • Services run in your app’s process in the main thread. Don’t block the main thread. Use a separate thread to do any heavy work in your Service.
  • You can also use Services running in another app’s process.
  • Services continue to run until you stop them. The system can also kill them at any time but you can set their priority so that they are unlikely to be killed.

So what happens when we bind to a Service?

Services Tutorial project filesOut in the open: The foreground Service

Android foreground service tutorial icon

It’s unlikely that the system will kill a foreground Service.

Typically we’d use foreground Services for work that the user is aware of, like playing music.

When we use a foreground Service, we have to send a notification to:

  • let the user know that the Service is running
  • give the user the opportunity to stop it

If you haven’t already done so, you may want to have a look at the article All about Services and Part 1 of the series of Services tutorials, A Simple Service

Doing it in the background. Services Tutorials. Part 1: A Simple Service

Android simple service tutorial icon

Our tutorial app shows you how to use Services to:

  • use a simple Service to update a user’s score on a server
  • use a foreground Service to play music in the background

There are two parts to the tutorial: Part 1 shows you how to use a simple Service and Part 2 shows you how to use a foreground Service.

All about Services

At your Service: Using Services

android services tutorial  icon

Services are app components that you can use to do work in the background. They don’t have a User Interface.

They are started and stopped by other components (like Activities, Broadcast Receivers and other Services).

You can also share Services with other apps.


Doing it on the quiet: how to use an IntentService

intentService iconYou should do time consuming work, like downloading files, on a separate thread.

This will free up the main thread so your user can carry on using the app.

IntentServices are the preferred way for doing long running background operations.

You can request an IntentService from any of your fragments or activities. The requests are queued and handled one-after-the-other. The IntentService stops once all the requests have been handled.