โš™๏ธ Advanced Android Engineering and Emerging Technology

Android Services and Foreground Services

Foreground services let your app keep doing user-visible work, like playing music, tracking a run or an active call, while the user does something else. Modern Android tightly restricts them: you must declare a type, hold permissions, show a notification, and justify use on Google Play.

โœ๏ธ Yunok Tech ๐Ÿ“… Updated โฑ 3 min read ๐ŸŽฏ Intermediate
On this page
  1. The modern rules at a glance
  2. Service types
  3. Manifest
  4. The service
  5. Background start restrictions
  6. Prefer alternatives when possible
  7. Troubleshooting
  8. Frequently asked questions
  9. Conclusion

A foreground service tells the system "the user knows this is running and cares about it". In return, Android keeps it alive. Because that power was abused, recent Android versions require explicit service types, matching permissions, and restrict when you can start one.

The modern rules at a glance

Requirements for a foreground service Declare the service with a foregroundServiceType in the manifest, request FOREGROUND_SERVICE and the type-specific permission, start it while the app is allowed (usually visible), call startForeground with a notification and type promptly, and stop it when the work ends. Declare typemanifest PermissionsFGS + type Start whenallowed startForeground+ notification Stopwhen done Plus: justify the type in Play Console when your app targets Android 14+.
Missing any step results in exceptions at runtime or Play review issues.

Service types

Since Android 14 (API 34), apps targeting it must declare at least one type per foreground service. Common types:

TypeForExtra permission
mediaPlaybackAudio/video playbackFOREGROUND_SERVICE_MEDIA_PLAYBACK
locationNavigation, run trackingFOREGROUND_SERVICE_LOCATION + location permission
phoneCallOngoing callsFOREGROUND_SERVICE_PHONE_CALL + role/permission
dataSyncUpload/download/syncFOREGROUND_SERVICE_DATA_SYNC (time-limited)
shortServiceQuick critical tasksNone (about 3 minutes, strict)
specialUseValid cases not covered by other typesFOREGROUND_SERVICE_SPECIAL_USE + justification

See the official types list for camera, microphone, health, connectedDevice, remoteMessaging and others, with their exact requirements. Some types, such as dataSync and mediaProcessing, have runtime limits on newer Android versions. Check the timeouts page.

Manifest

AndroidManifest.xmlXML
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application>
    <service
        android:name=".PlaybackService"
        android:exported="false"
        android:foregroundServiceType="mediaPlayback" />
</application>

The service

PlaybackService.ktKotlin
class PlaybackService : Service() {

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notification = NotificationCompat.Builder(this, CHANNEL_PLAYBACK)
            .setSmallIcon(R.drawable.ic_play)
            .setContentTitle("Playing")
            .setOngoing(true)
            .build()

        ServiceCompat.startForeground(
            this,
            NOTIFICATION_ID,
            notification,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK else 0,
        )
        // start the actual work (e.g. player) here
        return START_NOT_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? = null

    companion object {
        const val NOTIFICATION_ID = 42
        const val CHANNEL_PLAYBACK = "playback"
    }
}

// Starting it (while the app is in the foreground):
ContextCompat.startForegroundService(context, Intent(context, PlaybackService::class.java))
For media, prefer Media3

For playback, Media3's MediaSessionService handles the service, notification and media session for you.

Background start restrictions

Since Android 12, apps targeting it generally can't start foreground services while in the background. Doing so throws ForegroundServiceStartNotAllowedException. There are exemptions (for example, starting from a high-priority FCM message or after certain user interactions). Check the restrictions page. When in doubt, use WorkManager, which can promote long-running work with setForeground.

Prefer alternatives when possible

  • Deferrable work: WorkManager.
  • User-initiated data transfers (Android 14+): user-initiated data transfer jobs via JobScheduler for large uploads/downloads.
  • Exact alarms: AlarmManager for alarm clocks and reminders.

Troubleshooting

  • MissingForegroundServiceTypeException: type missing in the manifest or startForeground call.
  • SecurityException on start: type-specific permission or prerequisites (such as location permission) missing.
  • Notification not shown: on Android 13+ POST_NOTIFICATIONS may be denied. The service still runs, but appears in the Task Manager instead of the shade.
  • Service killed after a while: check type timeouts and call stopSelf() in onTimeout where required.

Frequently asked questions

Do I need a foreground service for a file upload?

Usually not. WorkManager (with constraints and retries) or a user-initiated data transfer job is preferred.

Will Play reject my app for foreground services?

Apps targeting Android 14+ must declare each type's use in Play Console. Use that doesn't match the type's purpose can be rejected.

Conclusion

Foreground services are for ongoing, user-visible work. Declare the right type and permissions, start while allowed, call startForeground promptly with a notification, respect timeouts, and prefer WorkManager or specialised APIs when they fit.

Next step: open specific screens from links with Deep Links and Android App Links.

Official references

๐Ÿ“ฑ Apps built by Yunok Tech

Real Android apps on Google Play, free to download.

See all our apps โ†’

Ready to build real Android applications?

Architecture makes the most sense when you apply it to a real project and see why each boundary exists. Learn how maintainable Android applications are designed by building one.

Move beyond theory and strengthen your Android development skills with Yunoktech's Android course. Learn core concepts, practical implementation, modern Android development techniques, and the reasoning behind building maintainable applications.

Explore the Android course and take your next step as an Android developer.

Share: LinkedIn X / Twitter WhatsApp