포그라운드 서비스 유형은 필수 항목임

To help developers be more intentional with defining user-facing foreground services, Android 10 introduced the android:foregroundServiceType attribute within the <service> element.

If your app targets Android 14, it must specify appropriate foreground service types. As in previous versions of Android, multiple types can be combined. This list shows the foreground service types to choose from:

If a use case in your app isn't associated with any of these types, we strongly recommend that you migrate your logic to use WorkManager or user-initiated data transfer jobs.

The health, remoteMessaging, shortService, specialUse, and systemExempted types are new in Android 14.

The following code snippet provides an example of a foreground service type declaration in the manifest:

<manifest ...>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
    <application ...>
      <service
          android:name=".MyMediaPlaybackService"
          android:foregroundServiceType="mediaPlayback"
          android:exported="false">
      </service>
    </application>
</manifest>

If an app that targets Android 14 doesn't define types for a given service in the manifest, then the system will raise MissingForegroundServiceTypeException upon calling startForeground() for that service.

포그라운드 서비스 유형을 사용할 새로운 권한 선언

If apps that target Android 14 use a foreground service, they must declare a specific permission, based on the foreground service type, that Android 14 introduces. These permissions appear in the sections labeled "permission that you must declare in your manifest file" in the intended use cases and enforcement for each foreground service type section on this page.

All of the permissions are defined as normal permissions and are granted by default. Users cannot revoke these permissions.

런타임에 포그라운드 서비스 유형 포함

The best practice for applications starting foreground services is to use the ServiceCompat version of startForeground() (available in androidx-core 1.12 and higher) where you pass in a bitwise integer of foreground service types. You can choose to pass one or more type values.

Usually, you should declare only the types required for a particular use case. This makes it easier to meet the system's expectations for each foreground service type. In cases where a foreground service is started with multiple types, then the foreground service must adhere to the platform enforcement requirements of all types.

ServiceCompat.startForeground(0, notification, FOREGROUND_SERVICE_TYPE_LOCATION)

If the foreground service type is not specified in the call, the type defaults to the values defined in the manifest. If you didn't specify the service type in the manifest, the system throws MissingForegroundServiceTypeException.

If the foreground service needs new permissions after you launch it, you should call startForeground() again and add the new service types. For example, suppose a fitness app runs a running-tracker service that always needs location information, but might or might not need media permissions. You would need to declare both location and mediaPlayback in the manifest. If a user starts a run and just wants their location tracked, your app should call startForeground() and pass just the location service type. Then, if the user wants to start playing audio, call startForeground() again and pass location|mediaPlayback.

시스템 런타임 검사

The system checks for proper use of foreground service types and confirms that the app has requested the proper runtime permissions or uses the required APIs. For instance, the system expects apps that use the foreground service type FOREGROUND_SERVICE_TYPE_LOCATION type to request either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.

This implies that apps must follow a very specific order of operations when requesting permissions from the user and starting foreground services. Permissions must be requested and granted before the app attempts to call startForeground(). Apps that request the appropriate permissions after the foreground service has been started must change this order of operations and request the permission before starting the foreground service.

The specifics of platform enforcement appear in the sections labeled "runtime requirements" in the intended use cases and enforcement for each foreground service type section on this page.

각 포그라운드 서비스 유형의 의도된 사용 사례 및 시행

지정된 포그라운드 서비스 유형을 사용하려면 매니페스트 파일에서 특정 권한을 선언하고 특정 런타임 요구사항을 충족해야 하며 앱은 해당 유형의 의도된 사용 사례 세트 중 하나를 충족해야 합니다. 다음 섹션에서는 선언해야 하는 권한, 런타임 기본 요건, 각 유형의 의도된 사용 사례를 설명합니다.

카메라

매니페스트에서 android:foregroundServiceType 아래 선언할 포그라운드 서비스 유형
camera
매니페스트에서 선언할 권한
FOREGROUND_SERVICE_CAMERA
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_CAMERA
런타임 기본 요건

CAMERA 런타임 권한 요청 및 부여

참고: CAMERA 런타임 권한에는 사용 중 제한사항이 적용됩니다. 이러한 이유로 몇 가지 예외를 제외하고 앱이 백그라운드에 있는 동안에는 camera 포그라운드 서비스를 만들 수 없습니다. 자세한 내용은 사용 중 권한이 필요한 포그라운드 서비스 시작 제한사항을 참고하세요.

설명

멀티태스킹을 허용하는 영상 채팅 앱과 같이 백그라운드에서 카메라에 계속 액세스합니다.

연결된 기기

Foreground service type to declare in manifest under
android:foregroundServiceType
connectedDevice
Permission to declare in your manifest
FOREGROUND_SERVICE_CONNECTED_DEVICE
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
Runtime prerequisites

At least one of the following conditions must be true:

Description

Interactions with external devices that require a Bluetooth, NFC, IR, USB, or network connection.

Alternatives

If your app needs to do continuous data transfer to an external device, consider using the companion device manager instead. Use the companion device presence API to help your app stay running while the companion device is in range.

If your app needs to scan for bluetooth devices, consider using the Bluetooth scan API instead.

데이터 동기화

Foreground service type to declare in manifest under
android:foregroundServiceType
dataSync
Permission to declare in your manifest
FOREGROUND_SERVICE_DATA_SYNC
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_DATA_SYNC
Runtime prerequisites
None
Description

Data transfer operations, such as the following:

  • Data upload or download
  • Backup-and-restore operations
  • Import or export operations
  • Fetch data
  • Local file processing
  • Transfer data between a device and the cloud over a network
Alternatives

See Alternatives to data sync foreground services for detailed information.

Health

要在其清单中声明的前台服务类型
android:foregroundServiceType
health
在清单中声明的权限
FOREGROUND_SERVICE_HEALTH
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_HEALTH
运行时前提条件

必须至少满足以下其中一个条件:

注意BODY_SENSORS 运行时权限受使用时限制。因此,您无法创建在应用在后台运行时使用身体传感器的 health 前台服务,但存在一些例外情况。如需了解详情,请参阅有关启动需要运行时权限的前台服务的限制

说明

为健身类别的应用(例如锻炼追踪器)提供支持的所有长时间运行的用例。

위치

매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceType
location
매니페스트에서 선언할 수 있는 권한
FOREGROUND_SERVICE_LOCATION
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_LOCATION
런타임 기본 요건

사용자가 위치 서비스를 사용 설정해야 하며 앱에 다음 런타임 권한 중 하나 이상이 부여되어야 합니다.

참고: 사용자가 위치 서비스를 사용 설정하고 런타임 권한에 대한 액세스 권한을 부여했는지 확인하려면 PermissionChecker#checkSelfPermission()를 사용하세요.

참고: 위치 런타임 권한에는 사용 중 제한사항이 적용됩니다. 따라서 ACCESS_BACKGROUND_LOCATION 런타임 권한을 부여받지 않는 한 앱이 백그라운드에 있는 동안 location 포그라운드 서비스를 만들 수 없습니다. 자세한 내용은 사용 중 권한이 필요한 포그라운드 서비스 시작에 관한 제한사항을 참고하세요.

설명

탐색 및 위치 공유와 같이 위치 액세스가 필요한 장기 실행 사용 사례

대안

사용자가 특정 위치에 도달할 때 앱을 트리거해야 한다면 대신 지오펜싱 API를 사용하는 것이 좋습니다.

미디어

Foreground service type to declare in manifest under
android:foregroundServiceType
mediaPlayback
Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PLAYBACK
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
Runtime prerequisites
None
Description
Continue audio or video playback from the background. Support Digital Video Recording (DVR) functionality on Android TV.
Alternatives
If you're showing picture-in-picture video, use Picture-in-Picture mode.

미디어 프로젝션

매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceType
mediaProjection
매니페스트에서 선언할 권한
FOREGROUND_SERVICE_MEDIA_PROJECTION
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
런타임 기본 요건

포그라운드 서비스를 시작하기 전에 createScreenCaptureIntent() 메서드를 호출합니다. 이렇게 하면 사용자에게 권한 알림이 표시됩니다. 사용자가 권한을 부여해야 서비스를 만들 수 있습니다.

포그라운드 서비스를 만든 후 MediaProjectionManager.getMediaProjection()를 호출할 수 있습니다.

설명

MediaProjection API를 사용하여 기본이 아닌 디스플레이 또는 외부 기기에 콘텐츠를 전송합니다. 이 콘텐츠는 반드시 미디어 콘텐츠일 필요는 없습니다.

대안

미디어를 다른 기기로 스트리밍하려면 Google Cast SDK를 사용하세요.

마이크

매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceType
microphone
매니페스트에서 선언할 권한
FOREGROUND_SERVICE_MICROPHONE
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_MICROPHONE
런타임 기본 요건

RECORD_AUDIO 런타임 권한을 요청하고 부여받습니다.

참고: RECORD_AUDIO 런타임 권한에는 사용 중 제한사항이 적용됩니다. 이러한 이유로 몇 가지 예외를 제외하고 앱이 백그라운드에 있는 동안에는 microphone 포그라운드 서비스를 만들 수 없습니다. 자세한 내용은 사용 중 권한이 필요한 포그라운드 서비스 시작 제한사항을 참고하세요.

설명

음성 녹음기 또는 통신 앱과 같이 백그라운드에서 마이크 캡처를 계속합니다.

전화 통화

Foreground service type to declare in manifest under
android:foregroundServiceType
phoneCall
Permission to declare in your manifest
FOREGROUND_SERVICE_PHONE_CALL
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_PHONE_CALL
Runtime prerequisites

At least one of these conditions must be true:

  • App is the default dialer app through the ROLE_DIALER role.
Description

Continue an ongoing call using the ConnectionService APIs.

Alternatives

If you need to make phone, video, or VoIP calls, consider using the android.telecom library.

Consider using CallScreeningService to screen calls.

원격 메시지

Foreground service type to declare in manifest under
android:foregroundServiceType
remoteMessaging
Permission to declare in your manifest
FOREGROUND_SERVICE_REMOTE_MESSAGING
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
Runtime prerequisites
None
Description
Transfer text messages from one device to another. Assists with continuity of a user's messaging tasks when they switch devices.

짧은 서비스

Foreground service type to declare in manifest under
android:foregroundServiceType
shortService
Permission to declare in your manifest
None
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
Runtime prerequisites
None
Description

Quickly finish critical work that cannot be interrupted or postponed.

This type has some unique characteristics:

  • Can only run for a short period of time (about 3 minutes).
  • No support for sticky foreground services.
  • Cannot start other foreground services.
  • Doesn't require a type-specific permission, though it still requires the FOREGROUND_SERVICE permission.
  • A shortService can only change to another service type if the app is currently eligible to start a new foreground service.
  • A foreground service can change its type to shortService at any time, at which point the timeout period begins.

The timeout for shortService begins from the moment that Service.startForeground() is called. The app is expected to call Service.stopSelf() or Service.stopForeground() before the timeout occurs. Otherwise, the new Service.onTimeout() is called, giving apps a brief opportunity to call stopSelf() or stopForeground() to stop their service.

A short time after Service.onTimeout() is called, the app enters a cached state and is no longer considered to be in the foreground, unless the user is actively interacting with the app. A short time after the app is cached and the service has not stopped, the app receives an ANR. The ANR message mentions FOREGROUND_SERVICE_TYPE_SHORT_SERVICE. For these reasons, it's considered best practice to implement the Service.onTimeout() callback.

The Service.onTimeout() callback doesn't exist on Android 13 and lower. If the same service runs on such devices, it doesn't receive a timeout, nor does it ANR. Make sure that your service stops as soon as it finishes the processing task, even if it hasn't received the Service.onTimeout() callback yet.

It's important to note that if the timeout of the shortService is not respected, the app will ANR even if it has other valid foreground services or other app lifecycle processes running.

If an app is visible to the user or satisfies one of the exemptions that allow foreground services to be started from the background, calling Service.StartForeground() again with the FOREGROUND_SERVICE_TYPE_SHORT_SERVICE parameter extends the timeout by another 3 minutes. If the app isn't visible to the user and doesn't satisfy one of the exemptions, any attempt to start another foreground service, regardless of type, causes a ForegroundServiceStartNotAllowedException.

If a user disables battery optimization for your app, it's still affected by the timeout of shortService FGS.

If you start a foreground service that includes the shortService type and another foreground service type, the system ignores the shortService type declaration. However, the service must still adhere to the prerequisites of the other declared types. For more information, see the Foreground services documentation.

특수 용도

매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceType
specialUse
매니페스트에서 선언할 권한
FOREGROUND_SERVICE_SPECIAL_USE
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
런타임 기본 요건
없음
설명

다른 포그라운드 서비스 유형에서 다루지 않는 유효한 포그라운드 서비스 사용 사례를 다룹니다.

개발자는 FOREGROUND_SERVICE_TYPE_SPECIAL_USE 포그라운드 서비스 유형을 선언하는 것 외에도 매니페스트에서 사용 사례를 선언해야 합니다. 이렇게 하려면 <service> 요소 내에서 <property> 요소를 지정해야 합니다. 이러한 값과 해당 사용 사례는 Google Play Console에서 앱을 제출할 때 검토됩니다. 제공하는 사용 사례는 자유 형식이므로 검토자가 specialUse 유형을 사용해야 하는 이유를 알 수 있도록 충분한 정보를 제공해야 합니다.

<service android:name="fooService" android:foregroundServiceType="specialUse">
  <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
      android:value="explanation_for_special_use"/>
</service>

시스템 제외됨

매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceType
systemExempted
매니페스트에서 선언할 권한
FOREGROUND_SERVICE_SYSTEM_EXEMPTED
startForeground()에 전달할 상수
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
런타임 기본 요건
없음
설명

포그라운드 서비스를 계속 사용하기 위해 시스템 애플리케이션 및 특정 시스템 통합을 위해 예약됩니다.

이 유형을 사용하려면 앱이 다음 기준 중 하나 이상을 충족해야 합니다.

포그라운드 서비스 유형 사용에 관한 Google Play 정책 시행

앱이 Android 14 이상을 타겟팅한다면 Play Console의 앱 콘텐츠 페이지 (정책 > 앱 콘텐츠)에서 앱의 포그라운드 서비스 유형을 선언해야 합니다. Play Console에서 포그라운드 서비스 유형을 선언하는 방법에 관한 자세한 내용은 포그라운드 서비스 및 전체 화면 인텐트 요구사항 이해를 참고하세요.