ネットワーク データ使用量を最適化する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
スマートフォンのライフサイクル全体では、モバイルデータ通信プランのコストがデバイス自体のコストを簡単に上回ります。Android 7.0(API レベル 24)以降では、デバイスのデータ使用量を最適化し、データの使用を抑えるためにデバイス全体でデータセーバーを有効にできます。この機能は、ローミング時、課金サイクルの終了間近、または短期間のデータパック利用時に特に有効です。
ユーザーが [設定] でデータセーバーを有効にし、デバイスが従量制課金ネットワークに接続されている場合、システムはバックグラウンドでのデータ使用をブロックし、フォアグラウンドでのデータ使用をなるべく抑えるようにアプリに指示します。特定のアプリでは、データセーバーがオンになっていても、ユーザーがバックグラウンドでの従量制課金接続を許可できます。
Android 7.0(API レベル 24)は、ConnectivityManager
API を拡張することで、ユーザーのデータセーバー設定を取得する方法と設定の変更を監視する方法をアプリに提供しています。アプリでユーザーがデータセーバーを有効にしているかどうかを確認し、フォアグラウンドとバックグラウンドでのデータ使用を抑えるようにすることをおすすめします。
データセーバー設定を確認する
Android 7.0(API レベル 24)以降では、アプリで ConnectivityManager
API を使用して、どのようなデータ使用制限が適用されているか確認できます。getRestrictBackgroundStatus()
メソッドは次のいずれかの値を返します。
-
RESTRICT_BACKGROUND_STATUS_DISABLED
- データセーバーが無効になっています。
-
RESTRICT_BACKGROUND_STATUS_ENABLED
- ユーザーがこのアプリのデータセーバーを有効にしています。アプリはフォアグラウンドでのデータ使用量を抑え、バックグラウンドでのデータ使用量に対する制限を適切に処理する必要があります。
-
RESTRICT_BACKGROUND_STATUS_WHITELISTED
-
ユーザーがデータセーバーを有効にしていますが、アプリはこれをバイパスできます。
アプリは、フォアグラウンドとバックグラウンドでのデータ使用量を引き続き抑えるようにする必要があります。
データセーバーが無効になっている場合や、アプリがデータセーバーをバイパスできる場合でも、デバイスが従量制課金ネットワークに接続されている場合はデータ使用量を抑えてください。以下のサンプルコードは、アプリで使用する必要があるデータ量を確認するために ConnectivityManager.isActiveNetworkMetered()
と ConnectivityManager.getRestrictBackgroundStatus()
を使用しています。
Kotlin
(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).apply {
// Checks if the device is on a metered network
if (isActiveNetworkMetered) {
// Checks user’s Data Saver settings.
when (restrictBackgroundStatus) {
RESTRICT_BACKGROUND_STATUS_ENABLED -> {
// Background data usage is blocked for this app. Wherever possible,
// the app should also use less data in the foreground.
}
RESTRICT_BACKGROUND_STATUS_WHITELISTED -> {
// The app is allowed to bypass Data Saver. Nevertheless, wherever possible,
// the app should use less data in the foreground and background.
}
RESTRICT_BACKGROUND_STATUS_DISABLED -> {
// Data Saver is disabled. Since the device is connected to a
// metered network, the app should use less data wherever possible.
}
}
} else {
// The device is not on a metered network.
// Use data as required to perform syncs, downloads, and updates.
}
}
Java
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// Checks if the device is on a metered network
if (connMgr.isActiveNetworkMetered()) {
// Checks user’s Data Saver settings.
switch (connMgr.getRestrictBackgroundStatus()) {
case RESTRICT_BACKGROUND_STATUS_ENABLED:
// Background data usage is blocked for this app. Wherever possible,
// the app should also use less data in the foreground.
case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
// The app is allowed to bypass Data Saver. Nevertheless, wherever possible,
// the app should use less data in the foreground and background.
case RESTRICT_BACKGROUND_STATUS_DISABLED:
// Data Saver is disabled. Since the device is connected to a
// metered network, the app should use less data wherever possible.
}
} else {
// The device is not on a metered network.
// Use data as required to perform syncs, downloads, and updates.
}
注: この動作は Android TV では異なります。Android TV はバックグラウンドでの使用をブロックするのではなく、使用量を調整するだけです。フォアグラウンドの場合は 800 Kbps、バックグラウンドの場合は 10 Kbps に制限されています。テレビのデータ使用量を制限するタイミングを検出するには、ConnectivityManager.isActiveNetworkMetered()
を使用します。
データ制限の権限をリクエストする
アプリがバックグラウンドでデータを使用する必要がある場合は、アプリのパッケージ名の URI を含む Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS
インテントを送信することで、データ制限の権限をリクエストできます(例: package:MY_APP_ID
)。
インテントと URI を送信すると、設定アプリが起動して、アプリのデータ使用量の設定が表示されます。これにより、ユーザーはアプリのバックグラウンド データを有効にするかどうかを決めることができます。このインテントを送信する前に、バックグラウンドでのデータ使用を有効にするために設定アプリを起動するかどうかを最初にユーザーに尋ねることをおすすめします。
データセーバー設定の変更を監視する
アプリでデータセーバー設定の変更を監視するには、BroadcastReceiver
を作成して ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED
をリッスンし、このレシーバを Context.registerReceiver()
で動的に登録します。
このブロードキャストを受信したアプリは、ConnectivityManager.getRestrictBackgroundStatus()
を呼び出して、新しいデータセーバー設定がその権限に影響を及ぼすかどうかを確認する必要があります。
注: このブロードキャストは、Context.registerReceiver()
を使用して動的に登録するアプリにのみ送信されます。
このブロードキャストの受信をマニフェストで登録しているアプリには送信されません。
Android Debug Bridge コマンドでテストする
Android Debug Bridge(ADB)には、データセーバーが有効な状態でアプリをテストするために使用できるコマンドがいくつか用意されています。ネットワーク パーミッションを確認、設定したり、定額制ネットワークでのアプリのテストに際してワイヤレス ネットワークを従量制に設定したりできます。
-
$ adb shell dumpsys netpolicy
- 現在のバックグラウンド ネットワーク全体の制限設定、データセーバーのバイパスが現在許可されているパッケージ UID、その他の既知のパッケージのネットワーク パーミッションを含むレポートが生成されます。
-
$ adb shell cmd netpolicy
- ネットワーク ポリシー マネージャー(netpolicy)のコマンド一覧が表示されます。
-
$ adb shell cmd netpolicy set restrict-background
<boolean>
true
または false
を渡したときに、それぞれ、データセーバー モードを有効または無効にします。
-
$ adb shell cmd netpolicy add restrict-background-whitelist
<UID>
- 指定されたパッケージ UID を許可リスト(
whitelist
)に追加し、バックグラウンドで従量制課金接続を使用できるようにします。
-
$ adb shell cmd netpolicy remove restrict-background-whitelist
<UID>
- 指定されたパッケージ UID を許可リスト(
whitelist
)から削除し、データセーバーが有効な場合にバックグラウンドでの従量制課金接続をブロックします。
-
$ adb shell cmd netpolicy list wifi-networks
- すべての Wi-Fi ネットワークについて従量制かどうかを一覧表示します。
-
$ adb shell cmd netpolicy set metered-network <WIFI_SSID>
true
- 指定された SSID の Wi-Fi を従量制に設定し、定額制ネットワークで従量制ネットワークをシミュレートできるようにします。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[],[],null,["# Optimize network data usage\n\nOver the life of a smartphone, the cost of a cellular data plan can easily\nexceed the cost of the device itself. On Android 7.0 (API level 24) and\nhigher, users can enable Data Saver on a device-wide basis in order to\noptimize their device's data usage, and use less data. This ability\nis especially useful when roaming, near the end of the billing cycle,\nor for a small prepaid data pack.\n\n\nWhen a user enables Data Saver in **Settings** and the device is\non a metered network, the system blocks background data usage and signals\napps to use less data in the foreground wherever possible. Users can\nallow specific apps to use background metered data usage even when Data\nSaver is turned on.\n\n\nAndroid 7.0 (API level 24) extends the\n[ConnectivityManager](/reference/android/net/ConnectivityManager)\nAPI to provide apps with a way to [retrieve the user's Data\nSaver preferences](#status) and [monitor preference\nchanges](#monitor-changes). It is considered good practice for apps to check whether the\nuser has enabled Data Saver and make an effort to limit foreground and\nbackground data usage.\n\nCheck data saver preferences\n----------------------------\n\n\nOn Android 7.0 (API level 24) and higher, apps can use the\n[ConnectivityManager](/reference/android/net/ConnectivityManager) API\nto determine what data usage restrictions are being applied. The\n[getRestrictBackgroundStatus()](/reference/android/net/ConnectivityManager#getRestrictBackgroundStatus())\nmethod returns one of the following values:\n\n\n`RESTRICT_BACKGROUND_STATUS_DISABLED`\n:\n Data Saver is disabled.\n\n\n`RESTRICT_BACKGROUND_STATUS_ENABLED`\n:\n The user has enabled Data Saver for this app. Apps should make an effort to limit data\n usage in the foreground and gracefully handle restrictions to background\n data usage.\n\n\n`RESTRICT_BACKGROUND_STATUS_WHITELISTED`\n:\n The user has enabled Data Saver but the app is allowed to bypass it.\n Apps should still make an effort to limit foreground and background data usage.\n\n\nLimit data usage whenever the device is connected to a metered network, even if Data Saver is\ndisabled or the app is allowed to bypass it. The following sample code uses\n[ConnectivityManager.isActiveNetworkMetered()](/reference/android/net/ConnectivityManager#isActiveNetworkMetered())\nand `ConnectivityManager.getRestrictBackgroundStatus()` to determine how much data the\napp should use: \n\n### Kotlin\n\n```kotlin\n(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).apply {\n // Checks if the device is on a metered network\n if (isActiveNetworkMetered) {\n // Checks user's Data Saver settings.\n when (restrictBackgroundStatus) {\n RESTRICT_BACKGROUND_STATUS_ENABLED -\u003e {\n // Background data usage is blocked for this app. Wherever possible,\n // the app should also use less data in the foreground.\n }\n RESTRICT_BACKGROUND_STATUS_WHITELISTED -\u003e {\n // The app is allowed to bypass Data Saver. Nevertheless, wherever possible,\n // the app should use less data in the foreground and background.\n }\n RESTRICT_BACKGROUND_STATUS_DISABLED -\u003e {\n // Data Saver is disabled. Since the device is connected to a\n // metered network, the app should use less data wherever possible.\n }\n }\n } else {\n // The device is not on a metered network.\n // Use data as required to perform syncs, downloads, and updates.\n }\n}\n```\n\n### Java\n\n```java\nConnectivityManager connMgr = (ConnectivityManager)\n getSystemService(Context.CONNECTIVITY_SERVICE);\n// Checks if the device is on a metered network\nif (connMgr.isActiveNetworkMetered()) {\n // Checks user's Data Saver settings.\n switch (connMgr.getRestrictBackgroundStatus()) {\n case RESTRICT_BACKGROUND_STATUS_ENABLED:\n // Background data usage is blocked for this app. Wherever possible,\n // the app should also use less data in the foreground.\n\n case RESTRICT_BACKGROUND_STATUS_WHITELISTED:\n // The app is allowed to bypass Data Saver. Nevertheless, wherever possible,\n // the app should use less data in the foreground and background.\n\n case RESTRICT_BACKGROUND_STATUS_DISABLED:\n // Data Saver is disabled. Since the device is connected to a\n // metered network, the app should use less data wherever possible.\n }\n} else {\n // The device is not on a metered network.\n // Use data as required to perform syncs, downloads, and updates.\n}\n```\n\n\n**Note:** This behavior is different on Android TV. Instead of\nblocking background usage, Android TV only throttles it. When in the\nforeground, applications are limited to 800 Kbps, and when in the background,\napplications are limited to 10 Kbps. Use\n`ConnectivityManager.isActiveNetworkMetered()` to detect when to\nlimit data usage on TV.\n\n### Request data restriction permissions\n\n\nIf your app needs to use data in the background, it can request data\nrestriction permissions by sending a\n`Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS`\nintent containing a URI of your app's package name: for example,\n`package:MY_APP_ID`.\n\n\nSending the intent and URI launches the **Settings** app and\ndisplays data usage settings for your app. The user can then decide whether\nto enable background data for your app. Before you send this intent, it is\ngood practice to first ask the user if they want to launch the\n**Settings** app for the purpose of enabling background data\nusage.\n\nMonitor changes to data saver preferences\n-----------------------------------------\n\n\nApps can monitor changes to Data Saver preferences by creating a\n[BroadcastReceiver](/reference/android/content/BroadcastReceiver) to\nlisten for `ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED` and dynamically\nregistering the receiver with\n[Context.registerReceiver()](/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)).\nWhen an app receives this broadcast, it should [check if the new Data Saver\npreferences affect its permissions](#status) by calling\n`ConnectivityManager.getRestrictBackgroundStatus()`.\n\n\n**Note:** The system only sends this broadcast to apps that dynamically register for\nthem with\n[Context.registerReceiver()](/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)).\nApps that register to receive this broadcast in their manifest will not receive them.\n\nTest with Android Debug Bridge commands\n---------------------------------------\n\n\nThe [Android Debug Bridge (ADB)](/tools/help/adb)\nprovides a few commands that you can use to test your app in Data Saver\nconditions. You can check and configure network\npermissions or set wireless networks as metered to test your app on unmetered\nnetworks.\n\n\n`$ adb shell dumpsys netpolicy`\n:\n Generates a report that includes the current global background network\n restriction setting, package UIDs that are currently allowed to bypass Data Saver, and the\n network permissions of other known packages.\n\n\n`$ adb shell cmd netpolicy`\n:\n Displays a full list of Network Policy Manager (netpolicy) commands.\n\n\n`$ adb shell cmd netpolicy set restrict-background\n\u003cboolean\u003e`\n:\n Enables or disables Data Saver mode when passing `true` or\n `false`, respectively.\n\n\n`$ adb shell cmd netpolicy add restrict-background-whitelist\n\u003cUID\u003e`\n:\n Adds the specified package UID to the allowlist (`whitelist`) to allow background\n metered data usage.\n\n\n`$ adb shell cmd netpolicy remove restrict-background-whitelist\n\u003cUID\u003e`\n:\n Removes the specified package UID from the allowlist (`whitelist`) to block\n background metered data usage while Data Saver is enabled.\n\n\n`$ adb shell cmd netpolicy list wifi-networks`\n:\n Lists all wifi networks, displaying whether they're metered.\n\n\n`$ adb shell cmd netpolicy set metered-network \u003cWIFI_SSID\u003e\ntrue`\n:\n Sets wifi with the specified SSID as metered, allowing you to simulate a\n metered network on an unmetered network."]]