DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
Mountain View desk6 min

How to Build an Android Foreground Service That Recovers After Process Death

Android can recreate a started foreground service after process death, but reliable recovery requires durable state, null-safe startup, and compliance with version-specific limits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A foreground service cannot be made immortal. To have Android recreate a started service after its process is killed, return START_STICKY from onStartCommand()—but treat that as a recovery request, not a promise of uninterrupted execution. Persist the work’s state outside the service process, handle a restarted service receiving a null intent, and resume only work that is still valid.

What “persists” means for a foreground service

Android may kill an app process even when it contains a foreground service. The service’s foreground status makes it more important to the system and requires a user-visible notification, but it does not prevent process death or override launch restrictions, permissions, service-type requirements, or runtime limits.

When a process dies, its in-memory objects disappear. A started service can be recreated according to the restart mode returned by onStartCommand(), but the new instance is not a continuation of the old one. It must recover from durable state. Android documents framework behavior; it does not establish identical restart timing across device makers.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the restart mode for the work

The return value controls what Android should do after process death. Choose based on whether the service can reconstruct ongoing work or needs the last start command attempted again.

Return value After process death Intent behavior Best fit
START_STICKY Android may recreate the started service. The last intent is not redelivered. If there are no pending start commands, the recreated service receives a call to onStartCommand() with a null intent. Ongoing work whose current state can be reconstructed independently, such as a media player waiting for more work. [Android Developers: Service; Services overview]
START_REDELIVER_INTENT Android schedules recreation. The last delivered intent is redelivered; the service can also inspect START_FLAG_REDELIVERY. An active discrete job that should be resumed from its last command, such as a download. Store checkpoints too: intent redelivery is not a durable task database. [Android Developers: Service; Services overview]
START_NOT_STICKY Android does not recreate the service solely because it had been started if there are no new start intents. No last-intent redelivery is requested; a later explicit start is needed. Work that should not resume without a fresh command, for example when repeating the previous action could be wrong. [Android Developers: Service]

Android’s API reference notes that the background-start restriction introduced for apps targeting Android 12 (API 31) or later does not prevent a sticky foreground service from restarting. That is different from the app initiating a new foreground-service start while it is in the background, which is generally restricted.

Make service startup safe to repeat

A sticky restart can reach onStartCommand() with null, and service creation can happen again after process death. Design initialization and task operations so that repeating them does not duplicate work, lose progress, or act on stale commands.

Keep task state outside the service process

Persist the information needed to decide what to do next: task identity, status, progress checkpoint, and any inputs that cannot be recovered elsewhere. On recreation, read that state and validate that the work is still requested and possible before resuming it. A start intent can identify or describe work; it should not be the only copy of essential progress.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle both fresh and restarted starts

Branch deliberately for a null intent, a fresh command, and a redelivered command. Make resume and stop operations idempotent: processing the same task twice should not create duplicate downloads, playback sessions, or side effects. If no valid task remains, stop the service rather than leaving an idle foreground notification indefinitely.

Checkpoint and make failure recoverable

Update durable progress at meaningful boundaries, and make external effects safe to retry or detect as already completed. Process death can occur between any two operations, so a checkpoint must not claim that an operation finished before its result is safely recorded.

Start and promote the service correctly

The usual launch sequence has two stages: the app calls Context.startForegroundService(), then the service calls ServiceCompat.startForeground() to promote itself and supply its notification. The notification should explain the ongoing user-visible task and provide relevant controls. Target-SDK prerequisites apply to creation and promotion, not just to the work performed afterward. [Android Developers: Launch a foreground service]

Check background-start rules before choosing a recovery path

For apps targeting API 31 or later, starting a foreground service while the app is in the background is generally prohibited unless a documented exception applies. Exceptions include returning from a user-visible state, user interaction with an app UI element such as a notification or widget, qualifying high-priority Firebase Cloud Messaging delivery, an exact alarm for a user-requested action, selected system broadcasts, and certain system roles or permissions. High-priority FCM messages can be downgraded; check the received priority before relying on that exception. [Android Developers: Launch a foreground service]

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not confuse that rule with a system restart of an already sticky foreground service: Android documents that the API 31 background-start restriction does not affect that restart. It still does not make a fresh background start by your app generally permissible.

While-in-use permissions need a visible start context

Camera, microphone, and location work may rely on while-in-use permissions. On Android 14 (API 34) and later, Android validates these requirements when the foreground service is created. A permission check can report that permission is granted even while the app is in the background, so that check alone does not show that the service can access the resource at that moment. Start this work while the app is visibly in use unless a documented exemption applies. [Android Developers: Launch a foreground service]

Meet the requirements for your target Android version

Target or platform level Requirement or limit What to account for
Target API 31+ (Android 12+) Background foreground-service starts are generally restricted, subject to documented exceptions. Separate an allowed system restart from an app-initiated start; verify the specific exception before relying on it. [Android Developers: Launch a foreground service]
Target API 34+ (Android 14+) Declare foreground-service types and request the corresponding foreground-service permissions. Declare the type that accurately matches the work. Missing requirements can cause service creation or promotion to fail. [Android Developers: Foreground service types]
Target API 35+ (Android 15+) dataSync and mediaProcessing foreground services are each limited to six hours per 24-hour period while the app is in the background. The clocks are tracked separately. Handle Service.onTimeout() when Android signals that a limit has been reached, and stop the service appropriately. Android 15 also restricts certain foreground-service types from being launched by a BOOT_COMPLETED receiver and narrows the SYSTEM_ALERT_WINDOW exception to apps with a visible overlay window. [Android Developers: Foreground service timeouts; Android 15 behavior changes]
Android 16 (API 36) Background jobs started from a foreground service must follow applicable job runtime quotas, including jobs started through JobScheduler and libraries such as WorkManager or DownloadManager. For user-triggered data transfers, consider the user-initiated data transfer job mechanism identified in Android’s change guidance. [Android 16 behavior changes: JobScheduler]
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a foreground service only when it fits the work

A foreground service is for work that is noticeable to the user and needs to run as foreground work now; it is not a general workaround for background limits. If the task is deferred, can be scheduled, or needs to survive without continuous user-visible execution, use an Android-supported background-work mechanism suited to that task. For user-triggered transfers on Android 16, the platform guidance points to user-initiated data transfer jobs; those jobs still have applicable runtime quotas.

Before implementation, decide whether the task is ongoing or discrete, whether it can be rebuilt from saved state, whether it must survive a device reboot, whether the app is visible when work begins, and which service type, permission, and runtime limits apply. A sticky restart addresses process recreation only; it does not by itself provide reboot recovery, durable progress, or permission to start work in every state.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Implementation review checklist

  • Return the restart mode that matches the task: reconstructable ongoing work, command redelivery, or no automatic restart.
  • Make the null-intent path explicit for sticky starts.
  • Persist task state and checkpoints outside service memory.
  • Make start, resume, retry, and stop safe to repeat.
  • Promote the service through the documented foreground-service sequence and show a useful notification.
  • Verify background-start eligibility and any while-in-use permission conditions at the moment of creation.
  • Declare the correct foreground-service type and its corresponding permission for the target SDK.
  • Implement applicable timeout and job-quota behavior instead of assuming foreground status removes limits.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.