ForegroundService Enum
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.
This enumeration supports a bitwise combination of its member values.
[System.Flags]
public enum ForegroundService
[<System.Flags>]
type ForegroundService =
- Inheritance
-
ForegroundService
- Attributes
Fields
| Name | Value | Description |
|---|---|---|
| TypeManifest | -1 | A special value indicates to use all types set in manifest file. Android reference for 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. |
| TypeNone | 0 | This constant was deprecated in API level 34. Do not use. The default foreground service type if not been set in manifest file. Apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later should NOT use this type, calling Service.startForeground(int, android.app.Notification, int) with this type will get a InvalidForegroundServiceTypeException. Android reference for 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. |
| TypeDataSync | 1 | Constant corresponding to dataSync in the R.attr.foregroundServiceType attribute. Data(photo, file, account) upload/download, backup/restore, import/export, fetch, transfer over network between device and cloud. This type has time limit of 6 hours starting from Android version Build.VERSION_CODES.VANILLA_ICE_CREAM. A foreground service of this type must be stopped within the timeout by Service.stopSelf(), Context.stopService(android.content.Intent) or their overloads). Service.stopForeground(int) will also work, which will demote the service to a "background" service, which will soon be stopped by the system. If the service isn't stopped within the timeout, Service.onTimeout(int, int) will be called. Also note, even though FOREGROUND_SERVICE_TYPE_DATA_SYNC can be used on Android versions prior to Build.VERSION_CODES.VANILLA_ICE_CREAM, since Service.onTimeout(int, int) did not exist on such versions, it will never be called. Because of this, developers must make sure to stop the foreground service even if Service.onTimeout(int, int) is not called on such versions. Android reference for 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. |
| TypeMediaPlayback | 2 | Constant corresponding to mediaPlayback in the R.attr.foregroundServiceType attribute. Music, video, news or other media playback. Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK. Android reference for 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. |
| TypePhoneCall | 4 | Constant corresponding to phoneCall in the R.attr.foregroundServiceType attribute. Ongoing operations related to phone calls, video conferencing, or similar interactive communication. Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_PHONE_CALL and Manifest.permission.MANAGE_OWN_CALLS or holding the default dialer role. Android reference for 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. |
| TypeLocation | 8 | Constant corresponding to location in the R.attr.foregroundServiceType attribute. GPS, map, navigation location update. Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_LOCATION and one of the following permissions: Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION. Android reference for 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. |
| TypeConnectedDevice | 16 | Constant corresponding to connectedDevice in the R.attr.foregroundServiceType attribute. Auto, bluetooth, TV or other devices connection, monitoring and interaction. Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE and one of the following permissions: Manifest.permission.BLUETOOTH_ADVERTISE, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, Manifest.permission.NFC, Manifest.permission.TRANSMIT_IR, Manifest.permission.UWB_RANGING, Manifest.permission.RANGING, or has been granted the access to one of the attached USB devices/accessories. Android reference for 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. |
| TypeMediaProjection | 32 | Constant corresponding to mediaProjection in the foregroundServiceType attribute. To capture through MediaProjection, an app must start a foreground service with the type corresponding to this constant. This type should only be used for MediaProjection. Capturing screen contents via createVirtualDisplay conveniently allows recording, presenting screen contents into a meeting, taking screenshots, or several other scenarios. Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION, and the user must have allowed the screen capture request from this app. Android reference for 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. |
| TypeCamera | 64 | Constant corresponding to camera in the R.attr.foregroundServiceType attribute. Use the camera device or record video. For apps with targetSdkVersion Build.VERSION_CODES.R and above, a foreground service will not be able to access the camera if this type is not specified in the manifest and in Service.startForeground(int, android.app.Notification, int). Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_CAMERA and Manifest.permission.CAMERA. Android reference for 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. |
| TypeMicrophone | 128 | Constant corresponding to microphone in the R.attr.foregroundServiceType attribute. Use the microphone device or record audio. For apps with targetSdkVersion Build.VERSION_CODES.R and above, a foreground service will not be able to access the microphone if this type is not specified in the manifest and in Service.startForeground(int, android.app.Notification, int). Starting foreground service with this type from apps targeting API level Build.VERSION_CODES.UPSIDE_DOWN_CAKE and later, will require permission Manifest.permission.FOREGROUND_SERVICE_MICROPHONE and one of the following permissions: Manifest.permission.CAPTURE_AUDIO_OUTPUT, Manifest.permission.RECORD_AUDIO. Android reference for 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. |
| TypeHealth | 256 | Constant corresponding to health in the R.attr.foregroundServiceType attribute. Health, wellness and fitness. The caller app is required to have the permissions Manifest.permission.FOREGROUND_SERVICE_HEALTH and one of the following permissions: Manifest.permission.ACTIVITY_RECOGNITION, Manifest.permission.HIGH_SAMPLING_RATE_SENSORS. HealthPermissions.READ_HEART_RATE, HealthPermissions.READ_SKIN_TEMPERATURE, HealthPermissions.READ_OXYGEN_SATURATION, HealthPermissions.READ_BLOOD_PRESSURE, HealthPermissions.READ_HEART_RATE_VARIABILITY, HealthPermissions.READ_RESPIRATORY_RATE, HealthPermissions.READ_VO2_MAX,. Requires Manifest.permission.FOREGROUND_SERVICE_HEALTH Android reference for 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. |
| TypeRemoteMessaging | 512 | Constant corresponding to remoteMessaging in the R.attr.foregroundServiceType attribute. Messaging use cases which host local server to relay messages across devices. Requires Manifest.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING Android reference for 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. |
| TypeSystemExempted | 1024 | Constant corresponding to systemExempted in the R.attr.foregroundServiceType attribute. The system exempted foreground service use cases. Note, apps are allowed to use this type only in the following cases: App has a UID < Process.FIRST_APPLICATION_UID; App is on Doze allowlist; Device is running in Demo Mode; Device owner app; Profile owner apps; Persistent apps; Carrier privileged apps; Apps that have the android.app.role.RoleManager#ROLE_EMERGENCY role; Headless system apps; Device admin apps; Active VPN apps; Apps holding Manifest.permission.SCHEDULE_EXACT_ALARM or Manifest.permission.USE_EXACT_ALARM permission. Requires Manifest.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED Android reference for 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. |
| TypeShortService | 2048 | A foreground service type for "short-lived" services, which corresponds to shortService in the R.attr.foregroundServiceType attribute in the manifest. Unlike other foreground service types, this type is not associated with a specific use case, and it will not require any special permissions (besides Manifest.permission.FOREGROUND_SERVICE). However, this type has the following restrictions. The type has a 3 minute timeout. A foreground service of this type must be stopped within the timeout by Service.stopSelf(), Context.stopService(android.content.Intent) or their overloads). Service.stopForeground(int) will also work, which will demote the service to a "background" service, which will soon be stopped by the system. If the service isn't stopped within the timeout, Service.onTimeout(int) will be called. Note, even when the system calls this callback, it will not stop the service automatically. You still need to stop the service using one of the aforementioned ways even when you get this callback. If the service is still not stopped after the callback, the app will be declared an ANR, after a short grace period of several seconds; A foreground service of this type cannot be made "sticky" (see Service.START_STICKY). That is, if an app is killed due to a crash or out-of memory while it's running a short foregorund-service, the system will not restart the service; Other foreground services cannot be started from short foreground services. Unlike other foreground service types, when an app is running in the background while only having a "short" foreground service, it's not allowed to start other foreground services, due to the restriction describe here: Restrictions on background starts; You can combine multiple foreground services types with |s, and you can combine FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. with other types as well. However, FOREGROUND_SERVICE_TYPE_SHORT_SERVICE is for situations where you have no other valid foreground services to use and the timeout is long enough for the task, and if you can use other types, there's no point using this type. For this reason, if FOREGROUND_SERVICE_TYPE_SHORT_SERVICE is combined with other foreground service types, the system will simply ignore it, and as a result, none of the above restrictions will apply (e.g. there'll be no timeout). Also note, even though FOREGROUND_SERVICE_TYPE_SHORT_SERVICE was added on Android version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, it can be also used on on prior android versions (just like other new foreground service types can be used). However, because Service.onTimeout(int) did not exist on prior versions, it will never called on such versions. Because of this, developers must make sure to stop the foreground service even if Service.onTimeout(int) is not called on such versions. Android reference for 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. |
| TypeMediaProcessing | 8192 | Constant corresponding to mediaProcessing in the R.attr.foregroundServiceType attribute. Media processing use cases such as video or photo editing and processing. This type has time limit of 6 hours. A foreground service of this type must be stopped within the timeout by Service.stopSelf(), Context.stopService(android.content.Intent) or their overloads). Service.stopForeground(int) will also work, which will demote the service to a "background" service, which will soon be stopped by the system. If the service isn't stopped within the timeout, Service.onTimeout(int, int) will be called. Also note, even though FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING was added in Android version Build.VERSION_CODES.VANILLA_ICE_CREAM, it can be also used on prior android versions (just like other new foreground service types can be used). However, because Service.onTimeout(int, int) did not exist on prior versions, it will never be called on such versions. Because of this, developers must make sure to stop the foreground service even if Service.onTimeout(int, int) is not called on such versions. Requires Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROCESSING Android reference for 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. |
| TypeSpecialUse | 1073741824 | Constant corresponding to specialUse in the R.attr.foregroundServiceType attribute. Use cases that can't be categorized into any other foreground service types, but also can't use JobInfo.Builder APIs. The use of this foreground service type may be restricted. Additionally, apps must declare a service-level <property> in AndroidManifest.xml as a hint of what the exact use case here is.
In a future release of Android, if the above foreground service type foo is supported by the platform, to offer the backward compatibility, the app could specify the android:maxSdkVersion attribute in the <uses-permission> section, and also add the foreground service type foo into the android:foregroundServiceType, therefore the same app could be installed in both platforms.
Requires Manifest.permission.FOREGROUND_SERVICE_SPECIAL_USE Android reference for 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. |