Working in the Background
What's in this Chapter?
Creating, starting, and stopping Services
Binding Services to Activities
Creating ongoing foreground Services
Extending the Intent Service
Using AsyncTasks to manage background processing
Creating background Threads and using Handlers to synchronize with the GUI Thread
Using Alarms to schedule application events
Android offers the Service class to create application components that handle long-lived operations and include functionality that doesn't require 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. When necessary a Service's priority can be raised to the equivalent of a foreground Activity. This is reserved for extreme cases, where the termination of a Service will noticeably affect the user experience—such as an interruption in music playback.
By using Services, you can ensure that your applications can continue to run even when their UI isn't visible.
Although Services run without a dedicated GUI, they still execute in the main Thread of the application's process—just like Activities and Broadcast Receivers. To keep your applications responsive, you'll learn to move time-consuming processes onto background Threads using the Thread and AsyncTask ...