ServiceInfo
open class ServiceInfo : ComponentInfo, Parcelable
kotlin.Any | |||
↳ | android.content.pm.PackageItemInfo | ||
↳ | android.content.pm.ComponentInfo | ||
↳ | android.content.pm.ServiceInfo |
Information you can retrieve about a particular application service. This corresponds to information collected from the AndroidManifest.xml's <service> tags.
Summary
Constants | |
---|---|
static Int |
Bit in |
static Int |
Bit in |
static Int |
Bit in |
static Int |
Bit in |
static Int |
Bit in |
static Int |
Bit in |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
A special value indicates to use all types set in manifest file. |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
The default foreground service type if not been set in manifest file. |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
static Int |
A foreground service type for "short-lived" services, which corresponds to |
static Int |
Constant corresponding to |
static Int |
Constant corresponding to |
Inherited constants | |
---|---|
Public constructors | |
---|---|
ServiceInfo(orig: ServiceInfo!) |
Public methods | |
---|---|
open Int | |
open Unit | |
open Int |
Return foreground service type specified in the manifest. |
open String |
toString() |
open Unit |
writeToParcel(dest: Parcel, parcelableFlags: Int) |
Inherited functions | |
---|---|
Properties | |
---|---|
static Parcelable.Creator<ServiceInfo!> | |
Int |
Options that have been set in the service declaration in the manifest. |
String! |
Optional name of a permission required to be able to access this Service. |
Inherited properties | |
---|---|
Constants
FLAG_ALLOW_SHARED_ISOLATED_PROCESS
static val FLAG_ALLOW_SHARED_ISOLATED_PROCESS: Int
Bit in flags
: If set, and this is an android.R.attr#isolatedProcess
service, the service is allowed to be bound in a shared isolated process with other isolated services. Note that these other isolated services can also belong to other apps from different vendors. Shared isolated processes are created when using the ) during service binding.
Value: 16
FLAG_EXTERNAL_SERVICE
static val FLAG_EXTERNAL_SERVICE: Int
Bit in flags
: If set, the service can be bound and run in the calling application's package, rather than the package in which it is declared. Set from android.R.attr#externalService
attribute.
Value: 4
FLAG_ISOLATED_PROCESS
static val FLAG_ISOLATED_PROCESS: Int
Bit in flags
: If set, the service will run in its own isolated process. Set from the android.R.attr#isolatedProcess
attribute.
Value: 2
FLAG_SINGLE_USER
static val FLAG_SINGLE_USER: Int
Bit in flags
: If set, a single instance of the service will run for all users on the device. Set from the android.R.attr#singleUser
attribute.
Value: 1073741824
FLAG_STOP_WITH_TASK
static val FLAG_STOP_WITH_TASK: Int
Bit in flags
: If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities. Set from the android.R.attr#stopWithTask
attribute.
Value: 1
FLAG_USE_APP_ZYGOTE
static val FLAG_USE_APP_ZYGOTE: Int
Bit in flags
: If set, the service (which must be isolated) will be spawned from an Application Zygote, instead of the regular Zygote. The Application Zygote will pre-initialize the application's class loader, and call a static callback into the application to allow it to perform application-specific preloads (such as loading a shared library). Therefore, spawning from the Application Zygote will typically reduce the service launch time and reduce its memory usage. The downside of using this flag is that you will have an additional process (the app zygote itself) that is taking up memory. Whether actual memory usage is improved therefore strongly depends on the number of isolated services that an application starts, and how much memory those services save by preloading. Therefore, it is recommended to measure memory usage under typical workloads to determine whether it makes sense to use this flag.
Value: 8
FOREGROUND_SERVICE_TYPE_CAMERA
static val FOREGROUND_SERVICE_TYPE_CAMERA: Int
Constant corresponding to camera
in the android.R.attr#foregroundServiceType
attribute. Use the camera device or record video. For apps with targetSdkVersion
android.os.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 android.app.Service#startForeground(int, android.app.Notification, int)
.
Starting foreground service with this type from apps targeting API level android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_CAMERA
and android.Manifest.permission#CAMERA
.
Value: 64
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
static val FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE: Int
Constant corresponding to connectedDevice
in the android.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 android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_CONNECTED_DEVICE
and one of the following permissions: android.Manifest.permission#BLUETOOTH_ADVERTISE
, android.Manifest.permission#BLUETOOTH_CONNECT
, android.Manifest.permission#BLUETOOTH_SCAN
, android.Manifest.permission#CHANGE_NETWORK_STATE
, android.Manifest.permission#CHANGE_WIFI_STATE
, android.Manifest.permission#CHANGE_WIFI_MULTICAST_STATE
, android.Manifest.permission#NFC
, android.Manifest.permission#TRANSMIT_IR
, android.Manifest.permission#UWB_RANGING
, android.Manifest.permission#RANGING
, or has been granted the access to one of the attached USB devices/accessories.
Value: 16
FOREGROUND_SERVICE_TYPE_DATA_SYNC
static val FOREGROUND_SERVICE_TYPE_DATA_SYNC: Int
Constant corresponding to dataSync
in the android.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 android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM
. A foreground service of this type must be stopped within the timeout by android.app.Service#stopSelf()
, android.content.Context#stopService(android.content.Intent)
or their overloads). android.app.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, android.app.Service#onTimeout(int, int)
will be called.
Also note, even though android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_DATA_SYNC
can be used on Android versions prior to android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM
, since android.app.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 android.app.Service#onTimeout(int, int)
is not called on such versions.
Value: 1
FOREGROUND_SERVICE_TYPE_HEALTH
static val FOREGROUND_SERVICE_TYPE_HEALTH: Int
Constant corresponding to health
in the android.R.attr#foregroundServiceType
attribute. Health, wellness and fitness.
The caller app is required to have the permissions android.Manifest.permission#FOREGROUND_SERVICE_HEALTH
and one of the following permissions: android.Manifest.permission#ACTIVITY_RECOGNITION
, android.Manifest.permission#HIGH_SAMPLING_RATE_SENSORS
. android.health.connect.HealthPermissions#READ_HEART_RATE
, android.health.connect.HealthPermissions#READ_SKIN_TEMPERATURE
, android.health.connect.HealthPermissions#READ_OXYGEN_SATURATION
,
Requires android.Manifest.permission#ACTIVITY_RECOGNITION
or android.Manifest.permission#HIGH_SAMPLING_RATE_SENSORS
or android.health.connect.HealthPermissions#READ_HEART_RATE
or android.health.connect.HealthPermissions#READ_SKIN_TEMPERATURE
or android.health.connect.HealthPermissions#READ_OXYGEN_SATURATION
Value: 256
FOREGROUND_SERVICE_TYPE_LOCATION
static val FOREGROUND_SERVICE_TYPE_LOCATION: Int
Constant corresponding to location
in the android.R.attr#foregroundServiceType
attribute. GPS, map, navigation location update.
Starting foreground service with this type from apps targeting API level android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_LOCATION
and one of the following permissions: android.Manifest.permission#ACCESS_COARSE_LOCATION
, android.Manifest.permission#ACCESS_FINE_LOCATION
.
Value: 8
FOREGROUND_SERVICE_TYPE_MANIFEST
static val FOREGROUND_SERVICE_TYPE_MANIFEST: Int
A special value indicates to use all types set in manifest file.
Value: -1
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
static val FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK: Int
Constant corresponding to mediaPlayback
in the android.R.attr#foregroundServiceType
attribute. Music, video, news or other media playback.
Starting foreground service with this type from apps targeting API level android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_MEDIA_PLAYBACK
.
Value: 2
FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
static val FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING: Int
Constant corresponding to mediaProcessing
in the android.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 android.app.Service#stopSelf()
, android.content.Context#stopService(android.content.Intent)
or their overloads). android.app.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, android.app.Service#onTimeout(int, int)
will be called.
Also note, even though android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
was added in Android version android.os.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 android.app.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 android.app.Service#onTimeout(int, int)
is not called on such versions.
Requires android.Manifest.permission#FOREGROUND_SERVICE_MEDIA_PROCESSING
Value: 8192
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
static val FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION: Int
Constant corresponding to mediaProjection
in the foregroundServiceType
attribute.
To capture through android.media.projection.MediaProjection
, an app must start a foreground service with the type corresponding to this constant. This type should only be used for android.media.projection.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 android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_MEDIA_PROJECTION
, and the user must have allowed the screen capture request from this app.
Value: 32
FOREGROUND_SERVICE_TYPE_MICROPHONE
static val FOREGROUND_SERVICE_TYPE_MICROPHONE: Int
Constant corresponding to microphone
in the android.R.attr#foregroundServiceType
attribute. Use the microphone device or record audio. For apps with targetSdkVersion
android.os.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 android.app.Service#startForeground(int, android.app.Notification, int)
.
Starting foreground service with this type from apps targeting API level android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_MICROPHONE
and one of the following permissions: android.Manifest.permission#CAPTURE_AUDIO_OUTPUT
, android.Manifest.permission#RECORD_AUDIO
.
Value: 128
FOREGROUND_SERVICE_TYPE_NONE
static valFOREGROUND_SERVICE_TYPE_NONE: Int
Deprecated: Do not use.
The default foreground service type if not been set in manifest file.
Apps targeting API level android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later should NOT use this type, calling android.app.Service#startForeground(int, android.app.Notification, int)
with this type will get a android.app.InvalidForegroundServiceTypeException
.
Value: 0
FOREGROUND_SERVICE_TYPE_PHONE_CALL
static val FOREGROUND_SERVICE_TYPE_PHONE_CALL: Int
Constant corresponding to phoneCall
in the android.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 android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE
and later, will require permission android.Manifest.permission#FOREGROUND_SERVICE_PHONE_CALL
and android.Manifest.permission#MANAGE_OWN_CALLS
or holding the default dialer role
.
Value: 4
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
static val FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING: Int
Constant corresponding to remoteMessaging
in the android.R.attr#foregroundServiceType
attribute. Messaging use cases which host local server to relay messages across devices.
Requires android.Manifest.permission#FOREGROUND_SERVICE_REMOTE_MESSAGING
Value: 512
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
static val FOREGROUND_SERVICE_TYPE_SHORT_SERVICE: Int
A foreground service type for "short-lived" services, which corresponds to shortService
in the android.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 android.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
android.app.Service#stopSelf()
,android.content.Context#stopService(android.content.Intent)
or their overloads).android.app.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,
android.app.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
android.app.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: android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. with other types as well. However,
android.content.pm.ServiceInfo#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, ifandroid.content.pm.ServiceInfo#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 android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
was added on Android version android.os.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 android.app.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 android.app.Service#onTimeout(int)
is not called on such versions.
Value: 2048
See Also
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
static val FOREGROUND_SERVICE_TYPE_SPECIAL_USE: Int
Constant corresponding to specialUse
in the android.R.attr#foregroundServiceType
attribute. Use cases that can't be categorized into any other foreground service types, but also can't use android.app.job.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. Here is an example:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <service android:name=".MySpecialForegroundService" android:foregroundServiceType="specialUse"> <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="foo" /> </service>
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.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" android:maxSdkVersion="last_sdk_version_without_type_foo" /> <service android:name=".MySpecialForegroundService" android:foregroundServiceType="specialUse|foo"> <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="foo" /> </service>
Requires
android.Manifest.permission#FOREGROUND_SERVICE_SPECIAL_USE
Value: 1073741824
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
static val FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED: Int
Constant corresponding to systemExempted
in the android.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 <
android.os.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
android.Manifest.permission#SCHEDULE_EXACT_ALARM
orandroid.Manifest.permission#USE_EXACT_ALARM
permission.
Requires
android.Manifest.permission#FOREGROUND_SERVICE_SYSTEM_EXEMPTED
Value: 1024
Public constructors
Public methods
describeContents
open fun describeContents(): Int
Return | |
---|---|
Int |
a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR |
getForegroundServiceType
open fun getForegroundServiceType(): Int
Return foreground service type specified in the manifest..
toString
open fun toString(): String
Return | |
---|---|
String |
a string representation of the object. |
writeToParcel
open fun writeToParcel(
dest: Parcel,
parcelableFlags: Int
): Unit
Parameters | |
---|---|
dest |
Parcel: The Parcel in which the object should be written. This value cannot be null . |
flags |
Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE . Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE , and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES |
Properties
flags
var flags: Int
Options that have been set in the service declaration in the manifest. These include: FLAG_STOP_WITH_TASK
, FLAG_ISOLATED_PROCESS
, FLAG_SINGLE_USER
.
permission
var permission: String!
Optional name of a permission required to be able to access this Service. From the "permission" attribute.