フォアグラウンド サービス タイプは必須

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.

フォアグラウンド サービス タイプを使用する新しい権限を宣言する

Android 14 をターゲットとするアプリでフォアグラウンド サービスを使用する場合、Android 14 で導入されたフォアグラウンド サービス タイプに基づいて、特定の権限を宣言する必要があります。これらの権限は、このページの対象ユースケースとフォアグラウンド サービス タイプごとの適用セクションの「マニフェスト ファイルで宣言する必要がある権限」に記載されています。

すべての権限は標準の権限として定義され、デフォルトで付与されます。ユーザーはこれらの権限を取り消すことはできません。

実行時にフォアグラウンド サービス タイプを含める

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.

各フォアグラウンド サービス タイプの対象ユースケースと適用

In order to use a given foreground service type, you must declare a particular permission in your manifest file, you must fulfill specific runtime requirements, and your app must fulfill one of the intended sets of use cases for that type. The following sections explain the permission that you must declare, the runtime prerequisites, and the intended use cases for each type.

カメラ

Foreground service type to declare in manifest under android:foregroundServiceType
camera
Permission to declare in your manifest
FOREGROUND_SERVICE_CAMERA
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_CAMERA
Runtime prerequisites

Request and be granted the CAMERA runtime permission

Note: The CAMERA runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a camera 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 to access the camera from the background, such as video chat apps that allow for multitasking.

接続されたデバイス

マニフェストで宣言するフォアグラウンド サービス タイプ
android:foregroundServiceType
connectedDevice
マニフェストで宣言する権限
FOREGROUND_SERVICE_CONNECTED_DEVICE
startForeground() に渡す定数
FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
ランタイムの前提条件

次の条件の少なくとも 1 つを満たす必要があります。

説明

Bluetooth、NFC、IR、USB、またはネットワーク接続を必要とする外部デバイスとの通信。

代替

アプリが外部デバイスへの継続的なデータ転送を行う必要がある場合は、代わりにコンパニオン デバイス マネージャーを使用することを検討してください。companion device presence API を使用すると、コンパニオン デバイスが範囲内にある間はアプリを実行し続けることができます。

アプリで Bluetooth デバイスをスキャンする必要がある場合は、代わりに Bluetooth scan API を使用することを検討してください。

データの同期

マニフェストで宣言するフォアグラウンド サービス タイプ
android:foregroundServiceType
dataSync
マニフェストで宣言する権限
FOREGROUND_SERVICE_DATA_SYNC
startForeground() に渡す定数
FOREGROUND_SERVICE_TYPE_DATA_SYNC
ランタイムの前提条件
なし
説明

次のようなデータ転送オペレーション:

  • データのアップロードまたはダウンロード
  • バックアップと復元オペレーション
  • インポート / エクスポート オペレーション
  • データの取得
  • ローカル ファイルの処理
  • ネットワーク経由でのデバイスとクラウド間のデータ転送
代替

詳細については、データ同期フォアグラウンド サービスに代わる方法をご覧ください。

健康

Foreground service type to declare in manifest under
android:foregroundServiceType
health
Permission to declare in your manifest
FOREGROUND_SERVICE_HEALTH
Constant to pass to startForeground()
FOREGROUND_SERVICE_TYPE_HEALTH
Runtime prerequisites

At least one of the following conditions must be true:

Note: The BODY_SENSORS runtime permission is subject to while-in-use restrictions. For this reason, you cannot create a health foreground service that uses body sensors 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

Any long-running use cases to support apps in the fitness category such as exercise trackers.

位置情報

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.

Media

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.

電話

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.

リモート メッセージ

マニフェストで宣言するフォアグラウンド サービス タイプ
android:foregroundServiceType
remoteMessaging
マニフェストで宣言する権限
FOREGROUND_SERVICE_REMOTE_MESSAGING
startForeground() に渡す定数
FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
ランタイムの前提条件
なし
説明
デバイス間でテキスト メッセージを転送します。これにより、ユーザーがデバイスを切り替えたときに、メッセージ タスクの継続性が確保されます。

ショート サービス

マニフェストで宣言するフォアグラウンド サービス タイプ
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 タイプの宣言を無視します。ただし、サービスは引き続き、他の宣言されたタイプの前提条件に準拠する必要があります。詳細については、フォアグラウンド サービスのドキュメントをご覧ください。

特殊用途

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>

除外されたシステム

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

Reserved for system applications and specific system integrations, to continue to use foreground services.

To use this type, an app must meet at least one of the following criteria:

  • Device is in demo mode state
  • App is a Device Owner
  • App is a Profiler Owner
  • Safety Apps that have the ROLE_EMERGENCY role
  • Device Admin apps
  • Apps holding SCHEDULE_EXACT_ALARM or USE_EXACT_ALARM permission and are using Foreground Service to continue alarms in the background, including haptics-only alarms.
  • VPN apps (configured using Settings > Network & Internet > VPN)

    Otherwise, declaring this type causes the system to throw a ForegroundServiceTypeNotAllowedException.

フォアグラウンド サービス タイプの使用に対する Google Play ポリシーの適用

Android 14 以降をターゲットとするアプリの場合は、Google Play Console の [アプリのコンテンツ] ページ([ポリシー] > [アプリのコンテンツ])で、アプリのフォアグラウンド サービスのタイプを申告する必要があります。Google Play Console でフォアグラウンド サービスのタイプを申告する方法について詳しくは、フォアグラウンド サービスと全画面インテントの要件についてをご覧ください。