In this chapter, we explored the very powerful Service
component, putting it to use to execute long-running background tasks with a configurable level of concurrency.
We learned that running tasks in a Service
gives them the best possible chance of successful completion if the user exits the application, because the system avoids killing processes with active Services unless absolutely necessary.
We discovered various ways to initiate background work in a Service
, from starting the Service
with an Intent
object through to direct method invocation using local Services.
We also saw the wide range of communication mechanisms available for delivering results back to the user: direct invocation of local Service
methods; sending Messages with Messenger
; broadcasting results to registered parties with BroadcastReceiver
; and, if the user has already left the application, raising system notifications.
Of course, we can also use PendingIntent
to send data back to the originating Activity, just as we did with IntentService
in Chapter 5, Queuing Work with IntentService.
In the next chapter, we'll add one last weapon to our arsenal: the ability to run background tasks at specific times—even when the device is asleep—by scheduling alarms with AlarmManager
.
3.139.72.254