Skip to content
tezvyn:

Android Foreground Services: Work the User Can See

Source: developer.android.comHardHow cards are made

Android Foreground Services: Work the User Can See

A foreground service tells Android, "Don't kill this process; the user knows it's running." It's a contract for long-running work made visible by a persistent notification, used for music playback or navigation.

Why it exists

To save battery and memory, Android is aggressive about terminating background processes. However, some tasks, like playing music or tracking a run, are initiated by the user and must continue even when the app is not on screen. Foreground services were created to solve this, providing a way for an app to signal to the OS that it's performing important, user-visible work that shouldn't be killed.

The mental model

Think of a foreground service as putting a "Do Not Disturb" sign on your app's background process. The OS sees the sign—a persistent notification—and understands the user is aware of the ongoing work. This makes your process a much lower priority for termination compared to a standard background service. It's a visible promise to the user that something is happening on their behalf.

How it works

First, you declare the FOREGROUND_SERVICE permission in your AndroidManifest.xml. For newer Android versions, you must also declare a more specific type, like FOREGROUND_SERVICE_MEDIA_PLAYBACK. You launch the service with a call to startForegroundService(). The critical step follows: within a short time limit (a few seconds), the service must call startForeground(), passing it a unique ID and a Notification object. This notification must be visible and is typically non-dismissible. If this call isn't made in time, the system will stop the service and flag the app with an Application Not Responding (ANR) error.

When to use it

Use foreground services for tasks that a user actively starts and expects to run to completion, regardless of app visibility. This includes continuous media playback, turn-by-turn navigation, tracking a fitness activity, or uploading a large file initiated by the user. The common thread is that the user is consciously aware of the task and its progress, which is reflected in the persistent notification.

When not to use it

Do not use a foreground service for any work that can be deferred or doesn't require immediate, continuous execution. For tasks like syncing data periodically or applying filters to a saved image, use WorkManager instead. Misusing foreground services creates a poor user experience with unnecessary notifications and can lead to OS-level restrictions on your app. Modern Android versions heavily restrict the ability to start foreground services while the app is in the background.

One canonical example

A podcast app playing an episode. When the user presses play and then switches to another app or locks their phone, the audio continues. A notification remains visible, showing the episode title and providing controls for pause, rewind, and stop. This is a foreground service of type mediaPlayback. The notification fulfills the contract by keeping the user informed and in control, justifying the service's high-priority status.

Interview question

What is the immediate and critical action required after starting an Android foreground service to ensure its proper operation?

  • a.Implement onStartCommand() to return START_STICKY to ensure the service restarts if killed by the system.
  • b.Bind the service to an activity to maintain its lifecycle connection to the UI.
  • c.Declare specific foreground service types in the manifest, like FOREGROUND_SERVICE_MEDIA_PLAYBACK.
  • d.Invoke startForeground() within a few seconds, providing a unique ID and a persistent notification.Correct
Why?

The card states that "within a short time limit (a few seconds), the service must call startForeground(), passing it a unique ID and a Notification object." This action signals to the OS that the user is aware of the ongoing work, preventing termination. Option C is a prerequisite, not an immediate action after starting the service.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles