Chapter 19. Bound Services and Permissions: Bound Together

Started services are great for background operations, but what if you need a service that’s more interactive? In this chapter you’ll discover how to create a bound service, a type of service your activity can interact with. You’ll see how to bind to the service when you need it, and how to unbind from it when you’re done to save resources. You’ll find out how to use Android’s Location Services to get location updates from your device GPS. Finally, you’ll discover how to use Android’s permission model, including handling runtime permission requests.

Bound services are bound to other components

As you saw in Chapter 18, a started service is one that starts when it’s passed an intent. It runs code in the background, and stops when the operation is complete. It continues running even if the component that starts it gets destroyed.

A bound service is one that’s bound to another application component, such as an activity. Unlike a started service, the component can interact with the bound service and call its methods.

To see this in action, we’re going to create a new odometer app that uses a bound service. We’ll use Android’s location service to track the distance traveled:

On the next page we’ll look at the steps we’ll go through to ...

Get Head First Android Development, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.