Don’t forget to share it with your network!
Suryaprakash Narsinghbhai Sharma
Sr Developer, Softices
Mobile Development
18 April, 2025
Suryaprakash Narsinghbhai Sharma
Sr Developer, Softices
Foreground services allow your app to perform critical tasks that are visible to the user and less likely to be killed by the system. However, Android has evolved significantly, and starting from Android 10+, developers must declare the type of foreground service and request specific permissions.
In this blog, we’ll explore:
A foreground service runs with a persistent notification, ensuring the user is aware of its operation.
Declared in the manifest:
<service
android:name=".YourService"
android:foregroundServiceType="location" />
Started in code:
startForegroundService(Intent(this, YourService::class.java))
CAMERA
android.permission.FOREGROUND_SERVICE_CAMERA
camera
BLUETOOTH_CONNECT
(Android 12+)
android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE
connectedDevice
android.permission.FOREGROUND_SERVICE_DATA_SYNC
dataSync
BODY_SENSORS
android.permission.FOREGROUND_SERVICE_HEALTH
health
ACCESS_FINE_LOCATION
and
ACCESS_BACKGROUND_LOCATION
(Android 10+)
android.permission.FOREGROUND_SERVICE_LOCATION
location
MEDIA_CONTENT_CONTROL
android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK
mediaPlayback
android.permission.FOREGROUND_SERVICE_MEDIA_PROCESSING
mediaProcessing
MediaProjectionManager
android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION
mediaProjection
RECORD_AUDIO
android.permission.FOREGROUND_SERVICE_MICROPHONE
microphone
CALL_PHONE
android.permission.FOREGROUND_SERVICE_PHONE_CALL
phoneCall
POST_NOTIFICATIONS
android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING
remoteMessaging
specialUse
systemExempted
To use any foreground service type from Android 14+, declare the right permission in the manifest:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
Some types (e.g., camera
,
microphone
,
location
) also need runtime
permission requests.
Different types of foreground services in Android have different rules when it comes to how long they are allowed to run. Some are strictly time-bound by the system (especially in Android 14/15+), while others can run indefinitely, provided user awareness is maintained through persistent notifications.
dataSync
dataSync
type have a strict
maximum runtime of 6 hours. After that, the system will automatically stop
the service, even if it’s still working.
WorkManager
or sync
adapters for long-term or recurring syncs where possible
location
mediaProcessing, camera, microphone
mediaProjection
mediaPlayback
location
,
remoteMessaging
) are allowed
without a visible activity. Others must show a foreground notification in
10 seconds or face
ServiceTimeoutException
.
location
,
mediaProcessing
, or
health
can drain battery
quickly if run for long periods.
SecurityException
at runtime.
foregroundServiceType
and
declare the permission in the manifest.
ActivityCompat.requestPermissions()
).
WorkManager
or
JobScheduler
when possible for
deferred tasks.
onTaskRemoved()
and fallback
persistence (e.g., Room + WorkManager).
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<service
android:name=".LocationService"
android:foregroundServiceType="location"
android:exported="false" />
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = createNotification()
startForeground(101, notification)
startLocationUpdates()
return START_STICKY
}
Foreground services are a powerful Android feature but come with tight restrictions, especially from Android 12 onward. Make sure you:
foregroundServiceType
.
FOREGROUND_SERVICE_*
permission
in the manifest.
WorkManager
when UI involvement
is minimal.