Our tutorial app uses a Service to play a sound clip.
We start a Service when the app starts. The Service will run as long as the app is alive.
We need to bind to the Service to start and stop playing a sound clip. We can leave the clip playing in the Service while it is running. It will continue to play even if the activity is paused or destroyed.
So what happens when we bind to a Service?
It’s unlikely that the system will kill a foreground Service.
Typically we’d use foreground Services for work that the user is aware of, like playing music.
When we use a foreground Service, we have to send a notification to:
Our tutorial app shows you how to use Services to:
There are two parts to the tutorial: Part 1 shows you how to use a simple Service and Part 2 shows you how to use a foreground Service.
Services are app components that you can use to do work in the background. They don’t have a User Interface.
They are started and stopped by other components (like Activities, Broadcast Receivers and other Services).
You can also share Services with other apps.
You should do time consuming work, like downloading files, on a separate thread.
This will free up the main thread so your user can carry on using the app.
IntentServices are the preferred way for doing long running background operations.
You can request an IntentService from any of your fragments or activities. The requests are queued and handled one-after-the-other. The IntentService stops once all the requests have been handled.