A foreground service performs some operation that is noticeable to the user. Foreground services must display a Notification.
Examples:
A music player app that plays music in a foreground service. The notification might show the current song being played.
A fitness app that records a user's run in a foreground service, after receiving permission from the user. The notification might show the distance that the user has traveled during the current fitness session.
when you launch a foreground service, the system checks for specific prerequisites based on service type. For example, if you try to launch a foreground service of type location, the system checks to make sure your app already has either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. If it doesn't, the system throws SecurityException.
In your app's manifest, declare each of your app's foreground services with a <service> element. For each service, use an android:foregroundServiceType attribute to declare what kind of work the service does.
For example, if your app creates a foreground service that plays music, you might declare the service like this:
If you try to create a foreground service and its type isn't declared in the manifest, the system throws a MissingForegroundServiceTypeException upon calling startForeground().
In addition, if the app targets API level 34 or higher, it must request the appropriate permission type for the kind of work the foreground service will be doing.
Each foreground service type has a corresponding permission type. For example, if an app launches a foreground service that uses the camera, you must request both the FOREGROUND_SERVICE and FOREGROUND_SERVICE_CAMERA permissions. These are all normal permissions, so the system grants them automatically if they're listed in the manifest.
Inside the service, usually in onStartCommand(), you can request that your service run in the foreground. To do so, call ServiceCompat.startForeground() (available in androidx-core 1.12 and higher). This method takes the following parameters:
The service
A positive integer that uniquely identifies the notification in the status bar
The Notification object itself
The foreground service types identifying the work done by the service
ServiceCompat.startForeground(
/* service = */ this,
/* id = */ 100, // Cannot be 0
/* notification = */ notification,
/* foregroundServiceType = */ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA
}
In older version there is a only startService(intent) method. Now .startForegroundService(intent) method is used. both works in a same way.
If you stop the service while it runs in the foreground, its notification is removed.
In the activity there is a method startForgroundService(intent). Intent object contains Service sub class having onstartcommand() overridden method.
startService or startForegroundService both works just make sure that you call .startForeground() method from service class, passed as a argument in the intent.
.startForeground() method calls generally from onStartCommand() method because it receives the intent as a parameter.
class ForegroundService: Service() {
override fun onCreate() {
super.onCreate()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForeground(
/* service = */ this,
/* id = */ 100, // Cannot be 0
/* notification = */ notification,
/* foregroundServiceType = */ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA
)
return START_STICKY
}
}
For many use cases, there are platform or Jetpack APIs you can use to do work you might otherwise use a foreground service for. If there is a suitable purpose-built API, you should almost always use it instead of using a foreground service. Purpose-built APIs often provide additional use-case specific capabilities that you would otherwise have to build on your own. For example, the Bubbles API handles the complex UI logic for messaging apps that need to implement chat-bubble features.
Note: In many cases, there are purpose-built APIs you can use instead of creating a particular type of foreground service. When purpose-built APIs are available, they are usually a better choice than creating a foreground service.
When you use a foreground service, you must display a notification so that users are actively aware that the service is running and is consuming system resources. users can dismiss the notification associated with a foreground service by default. To do so, users perform a swipe gesture on the notification. Traditionally, the notification isn't dismissed unless the foreground service is either stopped or removed from the foreground.
Note: The Workmanager API offers a flexible way of scheduling tasks, and is able to run these jobs as foreground service if needed. In many cases, using WorkManager is preferable to using foreground services directly.
Beginning with Android 14 (API level 34), you must declare an appropriate service type for each foreground service.