On this page
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
Service types
Since Android 14 (API 34), apps targeting it must declare at least one type per foreground service. Common types:
| Type | For | Extra permission |
|---|---|---|
mediaPlayback | Audio/video playback | FOREGROUND_SERVICE_MEDIA_PLAYBACK |
location | Navigation, run tracking | FOREGROUND_SERVICE_LOCATION + location permission |
phoneCall | Ongoing calls | FOREGROUND_SERVICE_PHONE_CALL + role/permission |
dataSync | Upload/download/sync | FOREGROUND_SERVICE_DATA_SYNC (time-limited) |
shortService | Quick critical tasks | None (about 3 minutes, strict) |
specialUse | Valid cases not covered by other types | FOREGROUND_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
<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
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 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 orstartForegroundcall.SecurityExceptionon 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()inonTimeoutwhere 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.
NotiVaultNotification history: recover dismissed notificationsGet it โธ
Walzi4K wallpapers for lock and home screenGet it โธ
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.