Service.StartForeground Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| StartForeground(Int32, Notification) |
If your service is started (running through Context.startService(Intent)), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. |
| StartForeground(Int32, Notification, ForegroundService) |
An overloaded version of startForeground(int,Notification) with additional foregroundServiceType parameter. |
StartForeground(Int32, Notification)
If your service is started (running through Context.startService(Intent)), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state.
[Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;)V", "")]
public void StartForeground(int id, Android.App.Notification? notification);
[<Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;)V", "")>]
member this.StartForeground : int * Android.App.Notification -> unit
Parameters
- id
- Int32
int: The identifier for this notification as per NotificationManager.notify(int, Notification); must not be 0.
- notification
- Notification
Notification: The Notification to be displayed.
- Attributes
Remarks
If your service is started (running through Context.startService(Intent)), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default started services are background, meaning that their process won't be given foreground CPU scheduling (unless something else in that process is foreground) and, if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You use startForeground(int, Notification) if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing. Note that calling this method does not put the service in the started state itself, even though the name sounds like it. You must always call startService(Intent) first to tell the system it should keep the service running, and then use this method to tell it to keep it running harder.
Apps targeting API Build.VERSION_CODES.P or later must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API.
Apps built with SDK version Build.VERSION_CODES.Q or later can specify the foreground service types using attribute R.attr.foregroundServiceType in service element of manifest file. The value of attribute R.attr.foregroundServiceType can be multiple flags ORed together.
Note: Beginning with SDK Version Build.VERSION_CODES.S, apps targeting SDK Version Build.VERSION_CODES.S or higher are not allowed to start foreground services from the background. See Behavior changes: Apps targeting Android 12 for more details. Note: Beginning with SDK Version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, apps targeting SDK Version Build.VERSION_CODES.UPSIDE_DOWN_CAKE or higher are not allowed to start foreground services without specifying a valid foreground service type in the manifest attribute R.attr.foregroundServiceType. See Behavior changes: Apps targeting Android 14 for more details.
See also:
Throws: ForegroundServiceStartNotAllowedException If the app targeting API is Build.VERSION_CODES.S or later, and the service is restricted from becoming foreground service due to background restriction.
Throws: InvalidForegroundServiceTypeException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later, and the manifest attribute R.attr.foregroundServiceType is set to invalid types(i.e. ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE).
Throws: MissingForegroundServiceTypeException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later, and the manifest attribute R.attr.foregroundServiceType is not set.
Throws: SecurityException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later and doesn't have the permission to start the foreground service with the specified type in the manifest attribute R.attr.foregroundServiceType.
Android reference for android.app.Service.startForeground.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Applies to
StartForeground(Int32, Notification, ForegroundService)
An overloaded version of startForeground(int,Notification) with additional foregroundServiceType parameter.
[Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;I)V", "", ApiSince=29)]
public void StartForeground(int id, Android.App.Notification notification, Android.Content.PM.ForegroundService foregroundServiceType);
[<Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;I)V", "", ApiSince=29)>]
member this.StartForeground : int * Android.App.Notification * Android.Content.PM.ForegroundService -> unit
Parameters
- id
- Int32
int: The identifier for this notification as per NotificationManager.notify(int, Notification); must not be 0.
- notification
- Notification
Notification: The Notification to be displayed. This value cannot be null.
- foregroundServiceType
- ForegroundService
int: must be a subset flags of manifest attribute R.attr.foregroundServiceType flags; must not be ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE. Value is either 0 or a combination of the following: ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
- Attributes
Remarks
An overloaded version of startForeground(int,Notification) with additional foregroundServiceType parameter. Apps built with SDK version Build.VERSION_CODES.Q or later can specify the foreground service types using attribute R.attr.foregroundServiceType in service element of manifest file. The value of attribute R.attr.foregroundServiceType can be multiple flags ORed together.
The foregroundServiceType parameter must be a subset flags of what is specified in manifest attribute R.attr.foregroundServiceType, if not, an IllegalArgumentException is thrown. Specify foregroundServiceType parameter as ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST to use all flags that is specified in manifest attribute foregroundServiceType.
Note: Beginning with SDK Version Build.VERSION_CODES.S, apps targeting SDK Version Build.VERSION_CODES.S or higher are not allowed to start foreground services from the background. See Behavior changes: Apps targeting Android 12 for more details. Note: Beginning with SDK Version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, apps targeting SDK Version Build.VERSION_CODES.UPSIDE_DOWN_CAKE or higher are not allowed to start foreground services without specifying a valid foreground service type in the manifest attribute R.attr.foregroundServiceType, and the parameter foregroundServiceType here must not be the ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE. See Behavior changes: Apps targeting Android 14 for more details.
See also:
Throws: ForegroundServiceStartNotAllowedException If the app targeting API is Build.VERSION_CODES.S or later, and the service is restricted from becoming foreground service due to background restriction.
Throws: InvalidForegroundServiceTypeException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later, and the manifest attribute R.attr.foregroundServiceType or the param foregroundServiceType is set to invalid types(i.e.ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE).
Throws: MissingForegroundServiceTypeException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later, and the manifest attribute R.attr.foregroundServiceType is not set and the param foregroundServiceType is set to ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST.
Throws: IllegalArgumentException if param foregroundServiceType is not subset of manifest attribute R.attr.foregroundServiceType.
Throws: SecurityException If the app targeting API is Build.VERSION_CODES.UPSIDE_DOWN_CAKE or later and doesn't have the permission to start the foreground service with the specified type in foregroundServiceType. R.attr.foregroundServiceType.
Android reference for android.app.Service.startForeground.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.