Building responsive apps with Service
Throughout this book, we have learned about the concurrency constructs provided by the Android platform for doing work off the main thread. So, it might seem surprising that, by itself, Service
does not provide any background threads and will run all of its callbacks directly on the main thread, just like Activity
.
If we perform long-running work or block the main thread in a Service
callback method, our application may be shut down, and a system-triggered Application Not Responding dialog is presented to the user.
While it is possible to configure the service to launch in a separate process, that process will still run the Service
callbacks on its own main thread and will be subject to the same constraints. The only difference is that our foreground process will not be shut down along with the misbehaving Service
process.
The solution, of course, is to pass the work off from the main thread to background "worker" threads.
IntentService
, which we learned...