The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Services and the Command Pattern

As noted previously, Android services are for long-running processes that may need to keep running even when decoupled from any activity. Examples include playing music even if the “player” activity is destroyed, polling the Internet for RSS/Atom feed updates, and maintaining an online chat connection even if the chat client loses focus due to an incoming phone call.

Services are created when manually started (via an API call) or when some activity tries connecting to the service via inter-process communication (IPC). Services will live until specifically shut down, until Android is desperate for RAM and terminates the process, or for a short period of time on Android 8.0+. Running for a long time has its costs, though, so services need to be careful not to use too much CPU or keep radios active too much of the time, lest the service cause the device’s battery to get used up too quickly.

This chapter outlines the basic theory behind creating and consuming services, including a look at the “command pattern” for services.

Why Services?

Services are a “Swiss Army knife” for a wide range of functions that do not require direct access to an activity’s user interface, such as:

  1. Performing operations that need to continue even if the user leaves the application’s activities, like a long download (as seen with the Play Store) or playing music (as seen with Android music apps)
  2. Performing operations that need to exist regardless of activities coming and going, such as maintaining a chat connection in support of a chat application
  3. Providing a local API to remote APIs, such as might be provided by a Web service
  4. Performing periodic work without user intervention, akin to cron jobs or Windows scheduled tasks

Even things like home screen app widgets often involve a service to assist with long-running work.

The primary role of a service is as a flag to the operating system, letting it know that your process is still doing work, despite the fact that it is in the background. This makes it somewhat less likely that Android will terminate your process due to low memory conditions.

Many applications will not need any services. Very few applications will need more than one. However, the service is a powerful tool for an Android developer’s toolbox and is a subject with which any qualified Android developer should be familiar.

Setting Up a Service

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

Communicating To Services

The preview of this section was fed to a gremlin, after midnight.

Scenario: The Music Player

The preview of this section is in the process of being translated from its native Klingon.

The Power of the PendingIntent

The preview of this section is being chased by zombies.

Communicating From Services

The preview of this section was lost in the sofa cushions.

Scenario: The Downloader

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

JobIntentService

The preview of this section took that left turn at Albuquerque.

IntentService or JobIntentService?

The preview of this section is in the process of being translated from its native Klingon.

Services and Configuration Changes

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

When Do Services End?

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

Background Service Limitations

The preview of this section is being chased by zombies.