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

デベロッパーがユーザー向けのフォアグラウンド サービスをより意識的に定義できるように、Android 10 で <service> 要素内に android:foregroundServiceType 属性が導入されました。

Android 14 をターゲットとするアプリでは、適切なフォアグラウンド サービス タイプを指定する必要があります。以前のバージョンの Android と同様に、複数のタイプを組み合わせることができます。以下に、選択可能なフォアグラウンド サービス タイプのリストを示します。

アプリのユースケースがこれらのタイプのいずれにも関連していない場合は、WorkManager またはユーザーが開始するデータ転送ジョブを使用するようにロジックを移行することを強くおすすめします。

Android 14 では、health, remoteMessaging, shortService, specialUse 型と systemExempted 型が新たに導入されています。

次のコード スニペットは、マニフェストでのフォアグラウンド サービス タイプの宣言の例を示しています。

<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 が発生します。

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

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.

システムによるランタイム チェック

フォアグラウンド サービスのタイプが適切な方法で使用されているかどうかは、システムによってチェックされます。また、アプリが適切な実行時権限をリクエストしているか、あるいは適切な API を使用しているかもチェックされます。たとえば、フォアグラウンド サービス タイプ FOREGROUND_SERVICE_TYPE_LOCATION を使用するアプリは、ACCESS_COARSE_LOCATION または ACCESS_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
ランタイムの前提条件
なし
説明

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

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

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

健康

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 and sensor-based READ runtime permissions are 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 unless you've been granted the BODY_SENSORS_BACKGROUND (API level 33 to 35) or READ_HEALTH_DATA_IN_BACKGROUND (API level 36 and higher) permissions. 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.

位置情報

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

ユーザーが位置情報サービスを有効にしており、アプリに次のいずれかの実行時の権限が付与されている必要があります。

注: ユーザーが位置情報サービスを有効にして実行時の権限へのアクセスを許可していることを確認するには、PermissionChecker#checkSelfPermission() を使用します。

注: 位置情報のランタイム権限には、使用中の制限が適用されます。そのため、ACCESS_BACKGROUND_LOCATION ランタイム権限が付与されている場合を除き、アプリがバックグラウンドにあるときに location フォアグラウンド サービスを作成することはできません。詳細については、使用中の権限が必要なフォアグラウンド サービスの開始に関する制限をご覧ください。

説明

ナビゲーションや現在地の共有など、位置情報へのアクセスが必要な長時間にわたるユースケース。

代替

ユーザーが特定の場所に到達したときにアプリをトリガーする必要がある場合は、代わりに geofence API の使用を検討してください。

メディア

マニフェストで宣言するフォアグラウンド サービス タイプ
android:foregroundServiceType
mediaPlayback
マニフェストで宣言する権限
FOREGROUND_SERVICE_MEDIA_PLAYBACK
startForeground() に渡す定数
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
ランタイムの前提条件
なし
説明
音声や動画のバックグラウンド再生を続行します。Android TV でデジタル動画録画(DVR)機能をサポートします。
代替
ピクチャー イン ピクチャー動画を表示する場合は、ピクチャー イン ピクチャー モードを使用します。

メディア プロジェクション

マニフェストの
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 フォアグラウンド サービスを作成することはできません。詳細については、使用中の権限が必要なフォアグラウンド サービスの開始に関する制限をご覧ください。

説明

ボイス レコーダーや通信アプリなど、バックグラウンドからマイクのキャプチャを続行します。

電話

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

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

  • アプリがマニフェスト ファイルで 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.

特殊用途

マニフェストで宣言するフォアグラウンド サービスのタイプ
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
ランタイムの前提条件
なし
説明

フォアグラウンド サービスを引き続き使用するために、システムアプリと特定のシステム統合用に予約されています。

このタイプを使用するには、アプリが次の条件の少なくとも 1 つを満たしている必要があります。

  • デバイスがデモモードの状態にある
  • アプリがデバイス所有者である
  • アプリがプロファイラ所有者である
  • ROLE_EMERGENCY のロールを持つ緊急情報アプリである
  • デバイス管理アプリである
  • SCHEDULE_EXACT_ALARM 権限または USE_EXACT_ALARM 権限を保持し、フォアグラウンド サービスを使用してハプティクスのみのアラームなど、バックグラウンドでアラームを継続するアプリ。
  • VPN アプリ([設定] > [ネットワークとインターネット] > [VPN] を使用して構成)

    そうしないと、このタイプを宣言したときに ForegroundServiceTypeNotAllowedException がスローされます。

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

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