共用方式為


ServiceInfo.ForegroundServiceTypeShortService Field

Definition

Caution

This constant will be removed in the future version. Use Android.Content.PM.ForegroundService enum directly instead of this field.

A foreground service type for "short-lived" services, which corresponds to shortService in the android.R.attr#foregroundServiceType attribute in the manifest.

[Android.Runtime.Register("FOREGROUND_SERVICE_TYPE_SHORT_SERVICE", ApiSince=34)]
[System.Obsolete("This constant will be removed in the future version. Use Android.Content.PM.ForegroundService enum directly instead of this field.", true)]
public const Android.Content.PM.ForegroundService ForegroundServiceTypeShortService = 2048;
[<Android.Runtime.Register("FOREGROUND_SERVICE_TYPE_SHORT_SERVICE", ApiSince=34)>]
[<System.Obsolete("This constant will be removed in the future version. Use Android.Content.PM.ForegroundService enum directly instead of this field.", true)>]
val mutable ForegroundServiceTypeShortService : Android.Content.PM.ForegroundService

Field Value

Value = 2048

Implements

Attributes

Remarks

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.

<ul> <li> 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. <li> 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. <li> 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 <li> You can combine multiple foreground services types with |s, and you can combine 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, if android.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). </ul>

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.

Java documentation for android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE.

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