Intern
What is the difference between a background service and a background service in Android?
sobes.tech AI
Answer from AI
The difference between a regular background service and a restricted (restricted) background service appeared with Android 8.0 (API 26).
Regular background service:
- Can run indefinitely in the background.
- The system can terminate it when resources are scarce, but it can be restarted if
START_STICKYwas returned inonStartCommand(). - Does not require visible notifications (though this is not best UX practice).
- Can consume significant battery and CPU resources.
Restricted background service (with restrictions):
- A service started in the background without visible activities or other "whitelists" (privileged conditions) faces resource usage restrictions.
- Main restrictions include:
- Background execution limit: The service can be stopped by the system shortly (usually a few minutes) after the app goes to background.
- Prohibition on starting new background services: Background apps cannot start new background services with
startService(). Instead,JobSchedulerorWorkManagershould be used. - Broadcast limitations: Implicit broadcasts in the manifest are ignored. Apps must register broadcast receivers explicitly or use JobScheduler/WorkManager.
- The goal of these restrictions is to improve battery life and device performance by limiting inefficient background resource usage.
How a service becomes unbounded (active in the eyes of the system):
- The app is in the foreground (user actively interacts with it).
- The app starts a visible service (foreground service), which shows a persistent notification.
- The app performs certain tasks (e.g., broadcast message processing, JobScheduler tasks) that temporarily lift it from a fully background state.
Main difference: From Android 8.0 and above, a service running in the background without user interaction or a visible service will be restricted by the system in terms of resource access and long-term operation. The regular "background service" before Android 8.0 could run without such restrictions.