Services and foreground service types

Lifecycle and app components Mid-level Kotlin 2.0+ API 34+ Reviewed By Best Latest Apps

Pick the fix

A fitness app tracks a run with GPS while the screen is off. Which approach fits current rules?

Options
Hint 1 of 1

Is the work deferrable, or must it run continuously now?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A A foreground service of type location, with its notification and permissions

Correct. Ongoing, user-visible location work is exactly what this type is for.

Why not the others?
  • B A periodic WorkManager job every minute. Periodic work runs at most every 15 minutes and isn't continuous.
  • C A plain background Service. It would be stopped shortly after the app leaves the foreground.
  • D An IntentService (deprecated since Android 11). It's deprecated and still a background service.

30-second answer

Use a foreground service for work the user is aware of and that must continue now, such as playing music, navigation or an active call. It must show a notification and, since Android 14, declare a foreground service type with the matching permission. For deferrable background work, use WorkManager instead. Background apps can't start foreground services except in specific allowed cases.

Practise saying it · 40 s. No recording, just a timer

Explain when to use a Service today.

Say your answer out loud, then check which of these points you covered:

  • Foreground service for user-visible ongoing work
  • Declare a service type (Android 14)
  • WorkManager for deferrable work
  • Background start restrictions

Deep dive

Types include mediaPlayback, location, camera, microphone, phoneCall, dataSync, connectedDevice and shortService.

dataSync has a daily time limit since Android 15; long transfers should move to WorkManager or user-initiated data transfer jobs.

Bound services let other components call into a running service; they stop when the last client unbinds, unless the service was also started, in which case it runs until stopSelf or stopService.

Call startForeground within a few seconds of startForegroundService, or the app is stopped with an error.

Promoting a service to the foreground Compile-checked · Kotlin 2.4.21
class RunService : Service() {
    override fun onBind(intent: Intent?): IBinder? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notification = NotificationCompat.Builder(this, "run")
            .setContentTitle("Recording your run")
            .setSmallIcon(android.R.drawable.ic_menu_mylocation)
            .build()
        ServiceCompat.startForeground(this, 1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
        return START_STICKY
    }
}

Common mistakes

  • Using a foreground service for periodic sync.
  • Forgetting the service type and crashing on Android 14.
  • Expecting a started background service to keep running; the system stops it soon after the app leaves the foreground.

Senior take & follow-ups

Show you know the current restrictions; old answers that suggest background services for sync are a red flag.

Follow-up questions

Think of your answer first, then open the hint.

  1. What replaces long dataSync services?

    Show hint

    WorkManager long-running workers or user-initiated data transfer jobs.

  2. What's a bound service for?

    Show hint

    Exposing an interface other components call while they're bound.

Suggest a fix if something here is wrong or out of date.