Interview frequency: …
Short answer
You must declare a specific service type (foregroundServiceType) in the manifest, request its specific permission, and launch a persistent notification visible to the user through startForeground within a few seconds.
Explanation
Since Android 14, it has become mandatory to specify the service type precisely (such as location, mediaPlayback, dataSync); and the system does not allow the service to launch if it does not match the declared permissions. In addition, if you do not call startForeground(id, notification) within 10 seconds of calling startForegroundService(), the app will crash immediately with a ForegroundServiceDidNotStartInTimeException.
Code example
<!-- In the file AndroidManifest.xml -->
<service
android:name=".PlaybackService"
android:foregroundServiceType="mediaPlayback" />
// In the class code:
override fun onCreate() {
super.onCreate()
val notification = createNotification()
startForeground(1001, notification)
}Common mistakes
- Delaying the call to startForeground() after launching the service, causing an immediate app crash.
- Using a generic service type without clear justification under Google Play store policies.