40
CHAPTER 4: Services
onHandleIntent()
method. Since basically you don’t need anything else,
the
IntentService
service is easy to implement. Note that starting with
Android 8.0 (API level 26), restrictions apply to background processes,
so under appropriate circumstances, consider using
JobIntentService
classes instead.
android.support.v4.app.JobIntentService
: This uses a
JobScheduler
to enqueue service execution requests. Starting with Android 8.0 (API
level 26), consider using this service base class for background services.
To implement such a service, you basically have to create a subclass
of
JobIntentService
and override the method
onHandleWork(intent:
Intent): Unit
to contain the service’s workload.
Services Lifecycle
Having described various service characteristics in the preceding sections, the actual
lifecycle of a service from a bird’s-view perspective is arguably easier than that of an activity.
However, be careful of services being able to run in the background. Also, because services
are more readily subject to stops forced by the Android OS, they may require special
attention in correspondence with service clients.
In your service implementation, you can overwrite any the lifecycle callbacks listed here, for
example, to log service invocation information while developing or debugging:
onCreate()
onStartCommand()
onBind()
onUnbind()
onRebind()
onDestroy()
41
CHAPTER 4: Services
Figure
4-1
shows an overview of the lifecycle of a service
Figure 4-1.
Service
lifecycle
|