Context.startForegroundService did not then call Service.startForeground
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Context.startForegroundService() followed by the error "did not then call Service.startForeground()" means Android started your service but the service failed to promote itself to the foreground quickly enough. On Android 8.0 and above, starting a foreground service creates a strict expectation that startForeground() will be called almost immediately with a visible notification.
Why Android Throws This Error
Foreground services are allowed to keep running because the user can see that they are active. If your app calls startForegroundService() and then delays too long before showing the notification, Android treats that as a broken contract and can kill the service with a RemoteServiceException.
In practice, this usually happens because:
- heavy initialization runs before
startForeground() - notification creation is delayed by I/O or dependency injection work
- the service exits or crashes before calling
startForeground() - logic calls
startForegroundService()for work that should not be a foreground service at all
The Correct Startup Pattern
Call startForeground() at the beginning of onStartCommand(), before expensive work.
The important part is that the notification is created and the service is promoted before doWork() begins.
Create the Notification Channel First
On Android 8.0 and above, the notification channel must exist before you post the foreground notification.
If channel setup fails or the notification build path throws, the service may never reach startForeground() and you will still get the same error.
When You Should Not Use a Foreground Service
Some work does not belong in a foreground service. If the task can be deferred or is guaranteed to finish quickly in the background, WorkManager is often the better choice.
Foreground services are appropriate for user-visible ongoing work such as navigation, active media playback, long-running uploads, or location tracking. Using them for ordinary background tasks increases complexity and runs into newer Android restrictions more easily.
Common Pitfalls
The biggest pitfall is performing network calls, dependency graph setup, database opens, or other heavy work before startForeground(). Any of those delays can trigger the exception.
Another issue is building the notification asynchronously. The foreground notification should be ready immediately, even if it is minimal and later updated with richer details.
Developers also sometimes call startForegroundService() and then rely on a later callback or coroutine to decide whether the service should really enter the foreground. That decision is too late. If the service was started as a foreground service, it must become one promptly.
Finally, modern Android versions impose additional restrictions on when foreground services can be started from the background. Even if you fix this specific error, make sure the service type and launch context still match current platform rules.
Summary
- '
startForegroundService()requires a near-immediate call tostartForeground().' - Build and post the notification before heavy work starts.
- Create the notification channel early on Android 8.0 and above.
- If the task is not truly user-visible ongoing work, consider
WorkManagerinstead. - This error usually means your service startup path is too slow or misses a code path that should call
startForeground().

