As mentioned in the introduction to this chapter, we need
a service to run as an always-on background process pulling the latest Twitter statuses into a local database.
The purpose of this pull mechanism is to cache updates locally so our
app can have data even when it’s offline. We’ll call this service
UpdaterService
.
Steps to creating a service are:
Create the Java class representing your service.
Register the service in the Android manifest file.
Start the service.
The basic procedure for
creating a service, as with activities and other main building blocks,
is to subclass a Service
class
provided by the Android framework.
To create the new service, we need to create a new Java file. Go ahead and select your Java package in the src folder, right-click and choose New→Class, and type in “UpdaterService” as the class name. This will create a new UpdaterService.java file as part of your package.
You may recall from Services that a typical service goes through the life cycle illustrated in Figure 8-1.
Figure 8-1. Service life cycle
Next, we want to override some of the main life cycle methods:
onCreate()
Called when the service is created for the first time
onStartCommand()
Called when the service is started
onDestroy()
Called when the service is terminated
To do that, you can use Eclipse tool Source→Override/Implement Methods and select those three ...
No credit card required