將網路數據用量最佳化
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用智慧型手機的期間,行動數據方案費用很容易計算
而不是裝置本身的成本在 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()
即可偵測啟動時間
限制電視的數據用量。
要求數據限制權限
如果應用程式需要在背景使用數據,可以要求取得資料
透過
Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS
包含應用程式套件名稱 URI 的意圖:例如
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>
-
從許可清單 (
whitelist
) 中移除要封鎖的指定套件 UID
數據節省模式啟用時的背景計量付費數據用量。
-
$ 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 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],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."]]