When and why use a Foreground Service on API 26+?

Tests background execution limits. Good answer: use for noticeable long-running work; declare FOREGROUND_SERVICE, show a notification, and respect API 26 background start limits. Red flag: calling it invisible work or omitting the notification.
What's really being asked
This question probes your understanding of Android's power management evolution and the boundary between acceptable background work and work that must be visible to the user. Interviewers want to see that you know why foreground services exist, when they are legally startable under modern restrictions, and what contractual obligations they impose on the app. It also surfaces whether you keep up with recent platform changes around service types and background start limitations.
The full answer
First, the why: a foreground service is appropriate for long-running work that the user actively notices and would expect to survive, such as music playback, turn-by-turn navigation, or a phone call. Second, the API 26 restrictions: starting a foreground service from the background is generally prohibited unless the app is in the foreground or qualifies for a specific exception like receiving a high priority FCM message or a broadcast. Third, the requirements: you must declare the FOREGROUND_SERVICE permission in the manifest, call startForeground within the service timeout after the service starts, and supply a persistent notification so the user is always aware of the ongoing work. Fourth, modern types: on newer releases you should declare a foregroundServiceType in the manifest and request the corresponding permission, such as location or microphone, aligning the service with the exact resource it uses.
The mistakes people make
A major red flag is suggesting a plain background service or WorkManager for user-noticeable continuous tasks, because WorkManager is designed for deferrable work and background services are heavily throttled on API 26 and above. Another mistake is forgetting the notification requirement or claiming the service can run silently; the platform requires a notification precisely because the work is user-facing. Candidates also stumble by not mentioning the manifest permission or the background-start restrictions, which suggests they have not shipped code on modern Android versions.
What usually comes next
The interviewer may ask how to handle the service timeout, what happens if startForeground is not called promptly, or how to stop the service correctly. They might also dig into the specific foregroundServiceType values, how to start a foreground service from a BroadcastReceiver, or the differences between startForegroundService and startService on pre-O and post-O devices. Be ready to discuss the user-stopped state and how to detect that the service was killed by the user swiping away the notification.
A concrete example
Suppose you are building a fitness tracker that records a GPS route while the screen is off. You would declare FOREGROUND_SERVICE and foregroundServiceType location in the manifest. When the user presses start, because your activity is in the foreground you call startForegroundService, then inside the service you immediately promote it via startForeground with a notification showing elapsed time and distance. On API 26 and above this keeps the process alive under background restrictions, whereas a plain background service would be killed by the system shortly after the activity stops.
Interview question
Which combination accurately describes the requirements for a valid foreground service on API 26+?
- a.Use WorkManager to launch the service, suppress the notification for silent operation, and skip the manifest declaration
- b.Declare FOREGROUND_SERVICE in the manifest, call startForeground with a notification within the timeout, and perform user-noticeable workCorrect
- c.Call startService from a background BroadcastReceiver and promote to foreground only when the user reopens the app
- d.Start the service from any background component, omit the notification, and allow the system to auto-grant all required permissions
Why? this is the answer
The card states that a foreground service requires the FOREGROUND_SERVICE manifest permission, a call to startForeground with a persistent notification within the service timeout, and must perform work the user actively notices. Option D is tempting because beginners often forget background-start restrictions and assume services can run silently, while D incorrectly delays startForeground and ignores API 26 background start limits.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #foreground-service
- #background-execution
- #api-26
- #services
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles