포그라운드 서비스 유형

Android 14 (API 수준 34)부터는 각 포그라운드 서비스에 적절한 서비스 유형을 선언해야 합니다. 즉, 앱 매니페스트에서 서비스 유형을 선언하고 해당 유형에 적합한 포그라운드 서비스 권한을 요청해야 합니다 (FOREGROUND_SERVICE 권한 요청 외에). 또한 포그라운드 서비스 유형에 따라 서비스를 실행하기 전에 런타임 권한을 요청해야 할 수도 있습니다.

카메라

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

CAMERA 런타임 권한을 요청하고 부여받음

설명

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

연결된 기기

要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
connectedDevice
在清单中声明的权限
FOREGROUND_SERVICE_CONNECTED_DEVICE
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
运行时前提条件

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

说明

与需要蓝牙、NFC、IR、USB 或网络连接的外部设备进行互动。

替代方案

如果您的应用需要向外部设备持续传输数据,请考虑改用配套设备管理器。使用配套设备感知 API,可帮助您的应用在配套设备在范围内时保持运行。

如果您的应用需要扫描蓝牙设备,请考虑改用 Bluetooth Scan API

데이터 동기화

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
运行时前提条件

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

说明

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

위치

Foreground service type to declare in manifest under
android:foregroundServiceType
location
Permission to declare in your manifest
FOREGROUND_SERVICE_LOCATION
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_LOCATION
Runtime prerequisites

The user must have enabled location services and the app must be granted at least one of the following runtime permissions:

Description

Long-running use cases that require location access, such as navigation and location sharing.

Alternatives

If your app needs to be triggered when the user reaches specific locations, consider using the geofence API instead.

미디어

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.

미디어 처리 중

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

Service for performing time-consuming operations on media assets, like converting media to different formats. The system allows this service a limited time to run; under normal circumstances, this time limit would be 6 hours out of every 24. (This limit is shared by all of an app's mediaProcessing foreground services.)

Your app should manually stop the media processing service in the following scenario:

If the timeout period is reached, the system calls the service's Service.onTimeout(int, int) method. At this time, the service has a few seconds to call Service.stopSelf(). If the service does not call Service.stopSelf(), an ANR will occur with this error message: "A foreground service of <fgs_type> did not stop within its timeout: <component_name>".

Note: Service.onTimeout(int, int) is not available on Android 14 or lower. On devices running those versions, if a media processing service reaches the timeout period, the system immediately caches the app. For this reason, your app shouldn't wait to get a timeout notification. Instead, it should terminate the foreground service or change it to a background service as soon as appropriate.

미디어 프로젝션

要在清单中声明的前台服务类型,位于
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 运行时权限。

说明

在后台继续捕获麦克风内容,例如录音器或通信应用。

전화 통화

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.

짧은 서비스

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

중단하거나 연기할 수 없는 중요한 작업을 신속하게 완료합니다.

이 유형에는 몇 가지 고유한 특성이 있습니다.

  • 짧은 기간(약 3분) 동안만 실행할 수 있습니다.
  • 고정 포그라운드 서비스는 지원되지 않습니다.
  • 다른 포그라운드 서비스는 시작할 수 없습니다.
  • 유형별 권한은 필요하지 않지만 FOREGROUND_SERVICE 권한은 필요합니다.
  • shortService는 앱이 현재 새 포그라운드 서비스를 시작할 수 있는 경우에만 다른 서비스 유형으로 변경할 수 있습니다.
  • 포그라운드 서비스는 언제든지 유형을 shortService로 변경할 수 있으며 이때 제한 시간 기간이 시작됩니다.

shortService의 제한 시간은 Service.startForeground()가 호출되는 시점부터 시작됩니다. 앱은 제한 시간이 발생하기 전에 Service.stopSelf() 또는 Service.stopForeground()를 호출해야 합니다. 그러지 않으면 새 Service.onTimeout()이 호출되어 앱이 stopSelf() 또는 stopForeground()를 호출하여 서비스를 중지할 수 있는 짧은 기회를 제공합니다.

Service.onTimeout()이 호출되고 잠시 후에 앱이 캐시된 상태가 되고 사용자가 앱과 활발하게 상호작용하지 않는 한 더 이상 포그라운드에 있는 것으로 간주되지 않습니다. 앱이 캐시되고 서비스가 중지되지 않은 잠시 후에 앱이 ANR을 수신합니다. ANR 메시지에서 FOREGROUND_SERVICE_TYPE_SHORT_SERVICE를 언급합니다. 이러한 이유로 Service.onTimeout() 콜백을 구현하는 것이 좋습니다.

Android 13 이하에는 Service.onTimeout() 콜백이 없습니다. 이러한 기기에서 동일한 서비스가 실행되는 경우 제한 시간과 ANR을 모두 수신하지 않습니다. 아직 Service.onTimeout() 콜백을 수신하지 않았더라도 처리 작업이 완료되는 즉시 서비스가 중지되는지 확인합니다.

shortService의 제한 시간이 준수되지 않으면 앱에 유효한 다른 포그라운드 서비스나 다른 앱 수명 주기 프로세스가 실행 중이더라도 ANR이 발생한다는 점에 유의해야 합니다.

앱이 사용자에게 표시되거나 포그라운드 서비스가 백그라운드에서 시작되도록 허용하는 예외 중 하나를 충족하는 경우 FOREGROUND_SERVICE_TYPE_SHORT_SERVICE 매개변수로 Service.StartForeground()를 다시 호출하면 제한 시간이 3분 더 연장됩니다. 앱이 사용자에게 표시되지 않고 예외 중 하나를 충족하지 않는 경우 유형과 관계없이 다른 포그라운드 서비스를 시작하려고 하면 ForegroundServiceStartNotAllowedException이 발생합니다.

사용자가 앱의 배터리 최적화를 사용 중지해도 shortService FGS의 제한 시간에 영향을 받습니다.

shortService 유형과 다른 포그라운드 서비스 유형이 포함된 포그라운드 서비스를 시작하면 시스템에서 shortService 유형 선언을 무시합니다. 그러나 서비스는 여전히 선언된 다른 유형의 기본 요건을 준수해야 합니다. 자세한 내용은 포그라운드 서비스 문서를 참고하세요.

특수 용도

매니페스트에서 선언할 포그라운드 서비스 유형
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에서 포그라운드 서비스 유형을 선언하는 방법에 관한 자세한 내용은 포그라운드 서비스 및 전체 화면 인텐트 요구사항 이해하기를 참고하세요.