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

개발자가 사용자 대상 포그라운드 서비스를 더 잘 정의하도록 돕기 위해 Android 10에서는 <service> 요소 내에 android:foregroundServiceType 속성을 도입했습니다.

앱이 Android 14를 타겟팅하는 경우 적절한 포그라운드 서비스 유형을 지정해야 합니다. 이전 버전의 Android에서와 마찬가지로 여러 유형을 결합할 수 있습니다. 이 목록에는 선택할 수 있는 포그라운드 서비스 유형이 표시됩니다.

앱의 사용 사례가 이러한 유형과 연결되어 있지 않으면 WorkManager 또는 사용자가 시작한 데이터 전송 작업을 사용하도록 로직을 이전하는 것이 좋습니다.

health, remoteMessaging, shortService, specialUsesystemExempted 유형은 Android 14에 새로 도입되었습니다.

다음 코드 스니펫은 매니페스트에서 포그라운드 서비스 유형을 선언하는 예를 보여줍니다.

<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>

Android 14를 타겟팅하는 앱이 매니페스트에서 특정 서비스의 유형을 정의하지 않으면 시스템은 해당 서비스의 startForeground() 호출 시 MissingForegroundServiceTypeException을 발생시킵니다.

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

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.

시스템 런타임 검사

系统会检查前台服务类型的使用是否恰当,并确认应用是否已请求适当的运行时权限或使用所需的 API。例如,系统希望使用前台服务类型 FOREGROUND_SERVICE_TYPE_LOCATION 的应用请求 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION

这意味着,在向用户请求权限和启动前台服务时,应用必须遵循非常具体的操作顺序。应用在尝试调用 startForeground() 之前,必须先请求并获得所需的权限。在启动前台服务后请求相应权限的应用必须更改此顺序,并在启动前台服务之前请求该权限。

本页面的每种前台服务类型的预期用例和强制执行部分中标记为“运行时要求”的部分列出了平台强制执行的具体内容。

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

为了使用给定的前台服务类型,您必须在清单文件中声明特定权限,必须满足特定的运行时要求,并且应用必须满足该类型的其中一组预期用例。以下部分介绍了您必须声明的权限、运行时前提条件以及每种类型的预期用例。

카메라

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.

데이터 동기화

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

다음과 같은 데이터 전송 작업입니다.

  • 데이터 업로드 또는 다운로드
  • 백업 및 복원 작업
  • 가져오기 또는 내보내기 작업
  • 데이터 가져오기
  • 로컬 파일 처리
  • 네트워크를 통해 기기와 클라우드 간에 데이터 전송
대안

자세한 내용은 데이터 동기화 포그라운드 서비스의 대안을 참고하세요.

Health

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

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

注意BODY_SENSORS 和基于传感器的读取运行时权限受“在使用时”限制。因此,除非您已获得 BODY_SENSORS_BACKGROUND(API 级别 33 到 35)或 READ_HEALTH_DATA_IN_BACKGROUND(API 级别 36 及更高级别)权限,否则您无法创建在应用处于后台运行时使用身体传感器的 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:

Note: In order to check that the user has enabled location services as well as granted access to the runtime permissions, use PermissionChecker#checkSelfPermission()

Note: The location runtime permissions are subject to while-in-use restrictions. For this reason, you cannot create a location foreground service while your app is in the background, unless you've been granted the ACCESS_BACKGROUND_LOCATION runtime permission. For more information, see Restrictions on starting foreground services that need while-in-use 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
mediaProjection
Permission to declare in your manifest
FOREGROUND_SERVICE_MEDIA_PROJECTION
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
Runtime prerequisites

Call the createScreenCaptureIntent() method before starting the foreground service. Doing so shows a permission notification to the user; the user must grant the permission before you can create the service.

After you have created the foreground service, you can call MediaProjectionManager.getMediaProjection().

Description

Project content to non-primary display or external device using the MediaProjection APIs. This content doesn't have to be exclusively media content.

Alternatives

To stream media to another device, use the Google Cast SDK.

마이크

Foreground service type to declare in manifest under
android:foregroundServiceType
microphone
Permission to declare in your manifest
FOREGROUND_SERVICE_MICROPHONE
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_MICROPHONE
Runtime prerequisites

Request and be granted the RECORD_AUDIO runtime permission.

Note: The RECORD_AUDIO runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a microphone foreground service while your app is in the background, with a few exceptions. For more information, see Restrictions on starting foreground services that need while-in-use permissions.

Description

Continue microphone capture from the background, such as voice recorders or communication apps.

전화 통화

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

다음 조건 중 하나 이상에 해당해야 합니다.

  • 앱이 매니페스트 파일에서 MANAGE_OWN_CALLS 권한을 선언했습니다.
  • 앱이 ROLE_DIALER 역할을 통해 기본 전화 앱입니다.
설명

ConnectionService API를 사용하여 진행 중인 통화를 계속합니다.

대안

전화, 영상 또는 VoIP 통화를 해야 하는 경우 android.telecom 라이브러리를 사용하는 것이 좋습니다.

CallScreeningService를 사용하여 통화를 선별하는 것이 좋습니다.

원격 메시지

要在清单中的以下位置声明的前台服务类型
android:foregroundServiceType
remoteMessaging
在清单中声明的权限
FOREGROUND_SERVICE_REMOTE_MESSAGING
要传递给 startForeground() 的常量
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
运行时前提条件
说明
将短信从一台设备转移到另一台设备。在用户切换设备时,帮助确保用户消息任务的连续性。

짧은 서비스

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.

특수 용도

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

Covers any valid foreground service use cases that aren't covered by the other foreground service types.

In addition to declaring the FOREGROUND_SERVICE_TYPE_SPECIAL_USE foreground service type, developers should declare use cases in the manifest. To do so, they specify the <property> element within the <service> element. These values and corresponding use cases are reviewed when you submit your app in the Google Play Console. The use cases you provide are free-form, and you should make sure to provide enough information to let the reviewer see why you need to use the specialUse type.

<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
런타임 기본 요건
없음
설명

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

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

  • 기기가 데모 모드 상태입니다.
  • 앱이 기기 소유자입니다.
  • 앱이 프로파일러 소유자입니다.
  • ROLE_EMERGENCY 역할이 있는 위급 정보 앱입니다.
  • 기기 관리자앱입니다.
  • SCHEDULE_EXACT_ALARM 또는 USE_EXACT_ALARM 권한을 보유하고 포그라운드 서비스를 사용하여 햅틱 전용 알람을 포함한 알람을 백그라운드에서 계속 실행하는 앱
  • VPN 앱 (설정 > 네트워크 및 인터넷 > VPN을 사용하여 구성됨)

    이 외의 경우에 이 유형을 선언하면 시스템에서 ForegroundServiceTypeNotAllowedException이 발생합니다.

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

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