Chapter 9. Working in the Background

WHAT'S IN THIS CHAPTER?

  • Creating, starting, and stopping Services

  • Binding Services to Activities

  • Setting Service priority to foreground

  • Using AsyncTasks to manage background processing

  • Creating background threads and using Handlers to synchronize with the GUI thread

  • Displaying Toasts

  • Using the Notification Manager to notify users of application events

  • Creating insistent and ongoing Notifications

  • Using Alarms to schedule application events

Android offers the Service class to create application components specifically to handle operations and functionality that should run invisibly, without a user interface.

Android accords Services a higher priority than inactive Activities, so they're less likely to be killed when the system requires resources. In fact, should the run time prematurely terminate a Service that's been started, it can be configured to restart as soon as sufficient resources become available. In extreme cases, the termination of a Service — such as an interruption in music playback — will noticeably affect the user experience, and in these cases a Service's priority can be raised to the equivalent of a foreground Activity.

By using Services, you can ensure that your applications continue to run and respond to events, even when they're not in active use.

Services run without a dedicated GUI, but, like Activities and Broadcast Receivers, they still execute in the main thread of the application's process. To help keep your applications responsive, ...

Get Professional Android™ 2 Application Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.