In Android, services are a type of component that runs in the background and performs long-running tasks without requiring any user interaction. They are used for operations that should continue even when the user switches to a different application or turns off the screen. Services can also define a remote interface using AIDL and provide some functionality to other apps. However, unlike system services, which are part of the OS and are always running, application services are started and stopped on demand.
Services can be used for a variety of tasks, such as playing music, downloading files, or monitoring sensors. They can also be used to perform tasks that require network access, such as sending or receiving data from a server.
Type of Services
-
Background Service
Background services are services that run in the background without any user interaction. They are typically used for tasks that are less important to the user, like syncing data with a server or updating the app's database. Unlike foreground services, background services do not require a notification to be shown to the user.
Pros
- Don't need to show a notification.
- They consume fewer system resources (like battery and memory) than foreground services, which can help improve device performance.
- They can be used to perform tasks that are not time-sensitive, like uploading a backup of user data to a server.
- Can have multiple background services running per app.
Cons
- Prone to be killed by the system when low on memory or app goes to background.
- Strict limits on CPU and network usage in recent Android versions to prevent battery drain.
-
Foreground Service
Foreground services are ongoing tasks that are noticeable to the user, often through a notification. For example, a music player app playing music or a navigation app providing directions are examples of foreground services. Here are some pros and cons of foreground services:
Pros
- Foreground services are visible to the user through notifications, which provides a sense of trust and transparency.
- Guaranteed to run by the system even when device is low on memory
- Not killed when app goes to background
Cons
- Persistent notifications can annoy users if they are not necessary or relevant to the user's needs.
- Foreground services might consume more battery as they are less likely to be killed by the system.
- Only one foreground service can run per app
Limitations Background Service on Various Android Versions
- On Android 6.0 (Marshmallow), Android introduced Doze mode, which imposes restrictions on background services to save battery.
- On Android 8 (Oreo), background services can only run for a maximum of 5 minutes at a time. After that, the system will kill the service.
- On Android 8 (Oreo), Background services are very prone to being killed when the device is under memory pressure.
- On Android 9 (Pie), Background services can only run for a maximum of 30 seconds at a time. After 30 seconds, the system kills the service.
- On Android 10 (Q), Background services are heavily rate-limited. They can only run for a few seconds at a time before being killed.
- Apps cannot continually restart background services. Frequent restarts will be blocked.
- Most use cases for background services should move to using
WorkManager or JobScheduler which have better battery optimization.
Limitations Background Service on Various Android Versions
- On Android 8 (Oreo), An app can only have one foreground service running at a time. If you start a new foreground service, the previous one is promoted to a background service.
- On Android 9 (Pie), Foreground services must show a notification to the user. The notification cannot be dismissed by the user.
- On Android 9 (Pie), The foreground service notification must be associated with an icon in the status bar. So the app must have an icon in the launcher.
- On Android 10 (Q), The foreground service notification title must clearly indicate that the foreground service is running. For example, "Music playing" or "File downloading".
- On Android 10 (Q), The notification must have a "Stop" or "Cancel" button to allow the user to stop the foreground service.
- On Android 10 (Q), The notification must have an "Ongoing" metadata field set to true. This displays an icon in the status bar indicating an ongoing notification.