Creating a Service

To create a service, you need to complete several steps. First, you create a class that extends Service. If all you need is a long-running task that runs in the background, there’s not much else you need to do. If you need to communicate with the service, you also need to override the onBind method, returning an instance of a class that implements the bindable methods. You can then communicate with that class by implementing the ServiceConnection class. Follow these steps.

1. Create a new package named services in the com.androiddevbook.onyourbike package. Use the New Class wizard to create a new class named TimerService that extends Service (see Listing 9.3).

Listing 9.3 Creating a New TimerService Class That Extends Service ...

Get Learning Android™ Application Programming: A Hands-On Guide to Building Android Applications 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.