Android development
foreground service
Service.startForeground
Context.startForegroundService
Android error handling

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.

kotlin
1class SyncService : Service() {
2
3    override fun onCreate() {
4        super.onCreate()
5        createNotificationChannel()
6    }
7
8    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
9        val notification = NotificationCompat.Builder(this, CHANNEL_ID)
10            .setContentTitle("Sync in progress")
11            .setContentText("Uploading files")
12            .setSmallIcon(R.drawable.ic_sync)
13            .build()
14
15        startForeground(NOTIFICATION_ID, notification)
16
17        Thread {
18            doWork()
19            stopForeground(STOP_FOREGROUND_REMOVE)
20            stopSelf()
21        }.start()
22
23        return START_NOT_STICKY
24    }
25
26    override fun onBind(intent: Intent?) = null
27}

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.

kotlin
1private fun createNotificationChannel() {
2    val channel = NotificationChannel(
3        CHANNEL_ID,
4        "Sync",
5        NotificationManager.IMPORTANCE_LOW
6    )
7
8    val manager = getSystemService(NotificationManager::class.java)
9    manager.createNotificationChannel(channel)
10}

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 to startForeground().'
  • 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 WorkManager instead.
  • This error usually means your service startup path is too slow or misses a code path that should call startForeground().

Course illustration
Course illustration

All Rights Reserved.