개발자가 사용자 대상 포그라운드 서비스를 더 잘 정의하도록 돕기 위해 Android 10에서는 <service> 요소 내에 android:foregroundServiceType 속성을 도입했습니다.
앱이 Android 14를 타겟팅하는 경우 적절한 포그라운드 서비스 유형을 지정해야 합니다. 이전 버전의 Android에서와 마찬가지로 여러 유형을 결합할 수 있습니다. 이 목록에는 선택할 수 있는 포그라운드 서비스 유형이 표시됩니다.
cameraconnectedDevicedataSynchealthlocationmediaPlaybackmediaProjectionmicrophonephoneCallremoteMessagingshortServicespecialUsesystemExempted
앱의 사용 사례가 이러한 유형과 연결되어 있지 않으면 WorkManager 또는 사용자가 시작한 데이터 전송 작업을 사용하도록 로직을 이전하는 것이 좋습니다.
health, remoteMessaging, shortService, specialUse 및 systemExempted 유형은 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.
시스템 런타임 검사
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_CAMERAstartForeground()에 전달할 상수FOREGROUND_SERVICE_TYPE_CAMERA- 런타임 기본 요건
CAMERA런타임 권한을 요청하고 부여받음참고:
CAMERA런타임 권한에는 사용 중 제한사항이 적용됩니다. 따라서 앱이 백그라운드에 있는 동안에는camera포그라운드 서비스를 만들 수 없습니다(일부 예외는 있음). 자세한 내용은 사용 중 권한이 필요한 포그라운드 서비스 시작에 관한 제한사항을 참고하세요.- 설명
멀티태스킹을 허용하는 영상 채팅 앱과 같이 백그라운드에서 카메라에 계속 액세스합니다.
연결된 기기
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceTypeconnectedDevice- 在清单中声明的权限
FOREGROUND_SERVICE_CONNECTED_DEVICE- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE- 运行时前提条件
必须至少满足以下其中一个条件:
在清单中至少声明以下其中一项权限:
至少请求并被授予以下其中一项运行时权限:
- 说明
与需要蓝牙、NFC、IR、USB 或网络连接的外部设备进行互动。
- 替代方案
如果您的应用需要向外部设备持续传输数据,请考虑改用配套设备管理器。使用配套设备感知 API,可帮助您的应用在配套设备在范围内时保持运行。
如果您的应用需要扫描蓝牙设备,请考虑改用 Bluetooth Scan API。
데이터 동기화
- 매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceTypedataSync- 매니페스트에서 선언할 권한
FOREGROUND_SERVICE_DATA_SYNCstartForeground()에 전달할 상수FOREGROUND_SERVICE_TYPE_DATA_SYNC- 런타임 기본 요건
- 없음
- 설명
다음과 같은 데이터 전송 작업입니다.
- 데이터 업로드 또는 다운로드
- 백업 및 복원 작업
- 가져오기 또는 내보내기 작업
- 데이터 가져오기
- 로컬 파일 처리
- 네트워크를 통해 기기와 클라우드 간에 데이터 전송
- 대안
자세한 내용은 데이터 동기화 포그라운드 서비스의 대안을 참고하세요.
Health
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceTypehealth- 在清单中声明的权限
FOREGROUND_SERVICE_HEALTH- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_HEALTH- 运行时前提条件
必须至少满足以下其中一个条件:
在清单中声明
HIGH_SAMPLING_RATE_SENSORS权限。至少请求并被授予以下其中一项运行时权限:
- 在 API 级别 35 及更低级别上使用
BODY_SENSORS READ_HEART_RATEREAD_SKIN_TEMPERATUREREAD_OXYGEN_SATURATIONACTIVITY_RECOGNITION
- 在 API 级别 35 及更低级别上使用
注意:
BODY_SENSORS和基于传感器的读取运行时权限受“在使用时”限制。因此,除非您已获得BODY_SENSORS_BACKGROUND(API 级别 33 到 35)或READ_HEALTH_DATA_IN_BACKGROUND(API 级别 36 及更高级别)权限,否则您无法创建在应用处于后台运行时使用身体传感器的health前台服务。如需了解详情,请参阅与启动需要使用时权限的前台服务相关的限制。- 说明
为健身类别的应用(例如锻炼追踪器)提供支持的所有长时间运行的用例。
위치
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceTypelocation- 在清单中声明的权限
FOREGROUND_SERVICE_LOCATION- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_LOCATION- 运行时前提条件
用户必须已启用位置信息服务,并且应用必须至少获得以下一项运行时权限:
注意:如需检查用户是否已启用位置信息服务以及是否已授予对运行时权限的访问权限,请使用
PermissionChecker#checkSelfPermission()注意:位置信息运行时权限受“使用期间”限制。因此,除非您已获得
ACCESS_BACKGROUND_LOCATION运行时权限,否则无法在应用在后台运行时创建location前台服务。如需了解详情,请参阅与启动需要“使用时”权限的前台服务相关的限制。- 说明
需要位置信息使用权的长时间运行的用例,例如导航和位置信息分享。
- 替代方案
如果您的应用需要在用户到达特定位置时触发,请考虑改用 Geofence API。
미디어
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceTypemediaPlayback- 在清单中声明的权限
FOREGROUND_SERVICE_MEDIA_PLAYBACK- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK- 运行时前提条件
- 无
- 说明
- 在后台继续播放音频或视频。在 Android TV 上支持数字视频录制 (DVR) 功能。
- 替代方案
- 如果您要显示画中画视频,请使用画中画模式。
미디어 프로젝션
- Foreground service type to declare in manifest under
android:foregroundServiceTypemediaProjection- 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
MediaProjectionAPIs. 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:foregroundServiceTypemicrophone- 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_AUDIOruntime permission.Note: The
RECORD_AUDIOruntime permission is subject to while-in-use restrictions. For this reason, you cannot create amicrophoneforeground 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:foregroundServiceTypephoneCall- 매니페스트에서 선언할 권한
FOREGROUND_SERVICE_PHONE_CALLstartForeground()에 전달할 상수FOREGROUND_SERVICE_TYPE_PHONE_CALL- 런타임 기본 요건
다음 조건 중 하나 이상에 해당해야 합니다.
- 앱이 매니페스트 파일에서
MANAGE_OWN_CALLS권한을 선언했습니다.
- 앱이 매니페스트 파일에서
- 앱이
ROLE_DIALER역할을 통해 기본 전화 앱입니다.
- 앱이
- 설명
ConnectionServiceAPI를 사용하여 진행 중인 통화를 계속합니다.- 대안
전화, 영상 또는 VoIP 통화를 해야 하는 경우
android.telecom라이브러리를 사용하는 것이 좋습니다.CallScreeningService를 사용하여 통화를 선별하는 것이 좋습니다.
원격 메시지
- 要在清单中的以下位置声明的前台服务类型
android:foregroundServiceTyperemoteMessaging- 在清单中声明的权限
FOREGROUND_SERVICE_REMOTE_MESSAGING- 要传递给
startForeground()的常量 FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING- 运行时前提条件
- 无
- 说明
- 将短信从一台设备转移到另一台设备。在用户切换设备时,帮助确保用户消息任务的连续性。
짧은 서비스
- 매니페스트에서 선언할 포그라운드 서비스 유형
android:foregroundServiceTypeshortService- 매니페스트에서 선언할 권한
- 없음
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유형 선언을 무시합니다. 그러나 서비스는 여전히 선언된 다른 유형의 기본 요건을 준수해야 합니다. 자세한 내용은 포그라운드 서비스 문서를 참고하세요.
특수 용도
- Foreground service type to declare in manifest under
android:foregroundServiceTypespecialUse- 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_USEforeground 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 thespecialUsetype.<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:foregroundServiceTypesystemExempted- 매니페스트에서 선언할 권한
FOREGROUND_SERVICE_SYSTEM_EXEMPTEDstartForeground()에 전달할 상수FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED- 런타임 기본 요건
- 없음
- 설명
포그라운드 서비스를 계속 사용하기 위해 시스템 애플리케이션과 특정 시스템 통합을 위해 예약됩니다.
이 유형을 사용하려면 앱이 다음 기준 중 하나 이상을 충족해야 합니다.
- 기기가 데모 모드 상태입니다.
- 앱이 기기 소유자입니다.
- 앱이 프로파일러 소유자입니다.
ROLE_EMERGENCY역할이 있는 위급 정보 앱입니다.- 기기 관리자앱입니다.
SCHEDULE_EXACT_ALARM또는USE_EXACT_ALARM권한을 보유하고 포그라운드 서비스를 사용하여 햅틱 전용 알람을 포함한 알람을 백그라운드에서 계속 실행하는 앱VPN 앱 (설정 > 네트워크 및 인터넷 > VPN을 사용하여 구성됨)
이 외의 경우에 이 유형을 선언하면 시스템에서
ForegroundServiceTypeNotAllowedException이 발생합니다.
포그라운드 서비스 유형 사용에 관한 Google Play 정책 시행
If your app targets Android 14 or higher, you'll need to declare your app's foreground service types in the Play Console's app content page (Policy > App content). For more information on how to declare your foreground service types in Play Console, see Understanding foreground service and full-screen intent requirements.