A service is a component in Android that can carry out work without having its own user interface, such as playing music, synchronising data or an ongoing file transfer. Together with activities, broadcast receivers and content providers, services are one of the four fundamental app components in Android, and like the others they must be registered in the app’s AndroidManifest.xml. By default, a service runs on the app’s main thread, so heavy work must still be moved to a background thread.
Android distinguishes between started services, which carry out a task until they are stopped, and bound services, which other components can bind to and communicate with. A foreground service carries out work that the user is aware of and must show a persistent notification. Because background work affects battery life and performance, Android has gradually introduced restrictions on when apps may start services in the background, and for deferrable work Google typically recommends WorkManager instead.
A service in Android is an app component that carries out work without its own user interface, such as music playback, synchronisation or file transfers. It can be started to carry out a task or bound by other components that want to communicate with it. Foreground services show a persistent notification so that the user knows the work is taking place, and because of Android's restrictions on background work, WorkManager is often used for deferrable tasks.
More about Service
A service is one of the four fundamental components of an Android app, together with activities, broadcast receivers and content providers. Whereas an activity shows a screen that the user interacts with, a service is used for work that does not require a user interface or that must continue even if the user leaves the screen. Typical examples are playing music or podcasts, navigation, uploading files and synchronising data with a server.
Technically, a service is a class that inherits from android.app.Service and is registered with a service element in AndroidManifest.xml. The system controls the service’s lifecycle through methods such as onCreate(), onStartCommand(), onBind() and onDestroy(). It is important to know that a service does not automatically run on a separate thread. By default, its code is executed on the app’s main thread, so long-running or blocking work must be moved to a background thread, for example with Kotlin coroutines, to prevent the app from becoming unresponsive.
Services can be used in different ways, each suited to different needs:
Foreground service: Carries out work that the user is aware of, such as music playback or active workout tracking. A foreground service must show a persistent notification, and in newer Android versions the app must declare a service type and hold the associated permissions.
Background service: Carries out work that the user does not see directly. Since Android 8.0, there have been strict restrictions on when apps may start such services while they are themselves in the background.
Bound service: Offers a client-server interface in which other components, including those in other apps, can bind to the service, send requests and receive responses. A bound service lives as long as at least one component is bound to it.
One of the greatest strengths of services is that they allow an app to continue important work without it being tied to a particular screen. At the same time, they require care, because work in the background uses battery, memory and data. Android has therefore gradually introduced rules that restrict background work, and users can see and stop apps that are running foreground services.
For many tasks, Google instead recommends WorkManager, which is part of Android Jetpack. WorkManager is intended for work that must be carried out reliably, even if the app is closed or the device is restarted, and that can be deferred, such as uploading log files or periodic synchronisation. WorkManager takes the system’s restrictions into account and can be configured with conditions such as a network connection or charging.
In summary, a service is the right choice when an app needs to carry out ongoing work that the user expects to continue, or when other components need to communicate with a long-running process. By choosing the right type of service, or WorkManager when the work can be deferred, developers can build apps that are both functional and gentle on the device’s resources.
Frequently asked questions about Service
What is a service in Android?
A service is a component in Android that carries out work in the background without having its own user interface. It is used for tasks that need to continue even when the user is not looking at the app, such as playing music, ongoing navigation or uploading files.
A service must be declared in the AndroidManifest.xml file. It is important to know that, by default, a service runs on the app’s main thread. Heavy tasks must therefore still be moved to a background thread or a coroutine. In newer Android versions, there are also strict rules on when apps may run in the background, in order to save battery.
What types of service are there in Android?
Android distinguishes between three types of service. A foreground service carries out a task that the user is aware of, and it must show a notification for as long as it runs. It is used, for example, for music playback or tracking a run.
A background service carries out work that the user does not see directly. Since Android 8.0, there have been strict restrictions on when apps may start such services, and WorkManager is recommended instead for many tasks. A bound service allows other components, such as an activity, to bind to it and communicate with it, and it only runs as long as something is bound to it.
What is the difference between a started service and a bound service?
A started service is started with startService or startForegroundService and runs until it stops itself or is stopped. It typically carries out a specific task and does not return a result directly to the component that started it.
A bound service is started when another component binds to it with bindService. It provides an interface so that the component can call methods and receive responses, for example an activity controlling a music player. When no components are bound any longer, it is stopped. A service can also be both at once if it is both started and bound.
When should WorkManager be used instead of a service?
WorkManager is Google’s recommended solution for background work that must be carried out reliably, even if the app is closed or the device is restarted. This can be synchronising data, uploading log files or periodic clean-up.
WorkManager respects the system’s rules on battery and background activity and can be set to run only when, for example, the device is on Wi-Fi or charging. A service, especially a foreground service, is more relevant when the work needs to happen immediately and is visible to the user, such as music playback or an ongoing call. For short-lived work while the app is open, coroutines are often sufficient.
Why must a foreground service show a notification?
A foreground service can run for a long time and use resources such as the processor, battery and location data while the user does not have the app open. Android therefore requires it to show a persistent notification, so that the user knows the app is working in the background.
This provides transparency and allows the user to stop the service or open the app. In newer Android versions, developers must also declare a type for the service, such as media playback, location or data sync, and hold the associated permissions. From Android 13, the user must also grant permission for notifications, which affects how the notification is shown.
What restrictions does Android place on background work?
To save battery and protect the user’s privacy, Android has gradually introduced restrictions on background work. Since Android 8.0, apps in the background may generally not start ordinary background services, and the system stops them after a short time.
Doze mode and App Standby defer network access and jobs when the device is not in use or the app is rarely used. Newer versions require types and permissions for foreground services and restrict when they may be started from the background. Access to location in the background requires a separate permission. Background work should therefore be scheduled with WorkManager whenever possible.
Service for dummies
A service in an Android app is a kind of invisible helper that can keep working even when nobody is looking at the app. Think of a music app: a song can be started, the user can go back to the home screen and write a message, and the music keeps playing. A service takes care of that. When an app uses a service for something the user should know about, the phone shows a small notification, so it is clear that the app is working. At the same time, Android makes sure that apps do not use too much power in the background, so the battery lasts longer.