ServiceInfo
public
class
ServiceInfo
extends ComponentInfo
implements
Parcelable
java.lang.Object | |||
↳ | 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 | |
---|---|
int |
FLAG_ALLOW_SHARED_ISOLATED_PROCESS
Bit in |
int |
FLAG_EXTERNAL_SERVICE
Bit in |
int |
FLAG_ISOLATED_PROCESS
Bit in |
int |
FLAG_SINGLE_USER
Bit in |
int |
FLAG_STOP_WITH_TASK
Bit in |
int |
FLAG_USE_APP_ZYGOTE
Bit in |
int |
FOREGROUND_SERVICE_TYPE_CAMERA
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_DATA_SYNC
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_HEALTH
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_LOCATION
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_MANIFEST
A special value indicates to use all types set in manifest file. |
int |
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_MICROPHONE
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_NONE
This constant was deprecated in API level 34. Do not use. |
int |
FOREGROUND_SERVICE_TYPE_PHONE_CALL
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
A foreground service type for "short-lived" services, which corresponds to
|
int |
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
Constant corresponding to |
int |
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
Constant corresponding to |
Inherited constants |
---|
Fields | |
---|---|
public
static
final
Creator<ServiceInfo> |
CREATOR
|
public
int |
flags
Options that have been set in the service declaration in the manifest. |
public
String |
permission
Optional name of a permission required to be able to access this Service. |
Inherited fields |
---|
Public constructors | |
---|---|
ServiceInfo()
|
|
ServiceInfo(ServiceInfo orig)
|
Public methods | |
---|---|
int
|
describeContents()
Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
void
|
dump(Printer pw, String prefix)
|
int
|
getForegroundServiceType()
Return foreground service type specified in the manifest.. |
String
|
toString()
Returns a string representation of the object. |
void
|
writeToParcel(Parcel dest, int parcelableFlags)
|
Inherited methods | |
---|---|
Constants
FLAG_ALLOW_SHARED_ISOLATED_PROCESS
public static final int FLAG_ALLOW_SHARED_ISOLATED_PROCESS
Bit in flags
: If set, and this is an 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
{@link android.content.Context#BIND_SHARED_ISOLATED_PROCESS) during service binding.
Note that when this flag is used, the {@link android.R.attr#process} attribute is
ignored when the process is bound into a shared isolated process by a client.
Constant Value: 16 (0x00000010)
FLAG_EXTERNAL_SERVICE
public static final int FLAG_EXTERNAL_SERVICE
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 R.attr.externalService
attribute.
Constant Value: 4 (0x00000004)
FLAG_ISOLATED_PROCESS
public static final int FLAG_ISOLATED_PROCESS
Bit in flags
: If set, the service will run in its own
isolated process. Set from the
R.attr.isolatedProcess
attribute.
Constant Value: 2 (0x00000002)
FLAG_SINGLE_USER
public static final int FLAG_SINGLE_USER
Bit in flags
: If set, a single instance of the service will
run for all users on the device. Set from the
R.attr.singleUser
attribute.
Constant Value: 1073741824 (0x40000000)
FLAG_STOP_WITH_TASK
public static final int FLAG_STOP_WITH_TASK
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
R.attr.stopWithTask
attribute.
Constant Value: 1 (0x00000001)
FLAG_USE_APP_ZYGOTE
public static final int FLAG_USE_APP_ZYGOTE
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.
Constant Value: 8 (0x00000008)
FOREGROUND_SERVICE_TYPE_CAMERA
public static final int FOREGROUND_SERVICE_TYPE_CAMERA
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
.
Constant Value: 64 (0x00000040)
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
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
,
or has been granted the access to one of the attached USB devices/accessories.
Constant Value: 16 (0x00000010)
FOREGROUND_SERVICE_TYPE_DATA_SYNC
public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC
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.
See also:
Constant Value: 1 (0x00000001)
FOREGROUND_SERVICE_TYPE_HEALTH
public static final int FOREGROUND_SERVICE_TYPE_HEALTH
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.BODY_SENSORS
,
Manifest.permission.HIGH_SAMPLING_RATE_SENSORS
.
Requires Manifest.permission.ACTIVITY_RECOGNITION
or Manifest.permission.BODY_SENSORS
or Manifest.permission.HIGH_SAMPLING_RATE_SENSORS
Constant Value: 256 (0x00000100)
FOREGROUND_SERVICE_TYPE_LOCATION
public static final int FOREGROUND_SERVICE_TYPE_LOCATION
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
.
Constant Value: 8 (0x00000008)
FOREGROUND_SERVICE_TYPE_MANIFEST
public static final int FOREGROUND_SERVICE_TYPE_MANIFEST
A special value indicates to use all types set in manifest file.
Constant Value: -1 (0xffffffff)
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
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
.
Constant Value: 2 (0x00000002)
FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
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
See also:
Constant Value: 8192 (0x00002000)
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
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.
Constant Value: 32 (0x00000020)
FOREGROUND_SERVICE_TYPE_MICROPHONE
public static final int FOREGROUND_SERVICE_TYPE_MICROPHONE
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
.
Constant Value: 128 (0x00000080)
FOREGROUND_SERVICE_TYPE_NONE
public static final int FOREGROUND_SERVICE_TYPE_NONE
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
.
Constant Value: 0 (0x00000000)
FOREGROUND_SERVICE_TYPE_PHONE_CALL
public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL
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
.
Constant Value: 4 (0x00000004)
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
public static final int FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
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
Constant Value: 512 (0x00000200)
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
public static final int FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
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:
|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, ifFOREGROUND_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.
See also:
Constant Value: 2048 (0x00000800)
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
public static final int FOREGROUND_SERVICE_TYPE_SPECIAL_USE
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.
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
Manifest.permission.FOREGROUND_SERVICE_SPECIAL_USE
Constant Value: 1073741824 (0x40000000)
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
public static final int FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
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
orManifest.permission.USE_EXACT_ALARM
permission.
Requires
Manifest.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED
Constant Value: 1024 (0x00000400)
Fields
flags
public int flags
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
public String permission
Optional name of a permission required to be able to access this Service. From the "permission" attribute.
Public constructors
Public methods
describeContents
public int describeContents ()
Describe the kinds of special objects contained in this Parcelable
instance's marshaled representation. For example, if the object will
include a file descriptor in the output of writeToParcel(android.os.Parcel, int)
,
the return value of this method must include the
CONTENTS_FILE_DESCRIPTOR
bit.
Returns | |
---|---|
int |
a bitmask indicating the set of special object types marshaled
by this Parcelable object instance.
Value is either 0 or CONTENTS_FILE_DESCRIPTOR |
dump
public void dump (Printer pw, String prefix)
Parameters | |
---|---|
pw |
Printer |
prefix |
String |
getForegroundServiceType
public int getForegroundServiceType ()
Return foreground service type specified in the manifest..
Returns | |
---|---|
int |
foreground service type specified in the manifest.
Value is either 0 or a combination of FOREGROUND_SERVICE_TYPE_MANIFEST , FOREGROUND_SERVICE_TYPE_NONE , FOREGROUND_SERVICE_TYPE_DATA_SYNC , FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK , FOREGROUND_SERVICE_TYPE_PHONE_CALL , FOREGROUND_SERVICE_TYPE_LOCATION , FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE , FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION , FOREGROUND_SERVICE_TYPE_CAMERA , FOREGROUND_SERVICE_TYPE_MICROPHONE , FOREGROUND_SERVICE_TYPE_HEALTH , FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING , FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED , FOREGROUND_SERVICE_TYPE_SHORT_SERVICE , android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_FILE_MANAGEMENT, FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING , and FOREGROUND_SERVICE_TYPE_SPECIAL_USE |
toString
public String toString ()
Returns a string representation of the object.
Returns | |
---|---|
String |
a string representation of the object. |
writeToParcel
public void writeToParcel (Parcel dest, int parcelableFlags)
Parameters | |
---|---|
dest |
Parcel |
parcelableFlags |
int |