เพิ่มประสิทธิภาพปริมาณการใช้อินเทอร์เน็ตของเครือข่าย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การใช้แผนข้อมูลเครือข่ายมือถือตลอดอายุการใช้งานสมาร์ทโฟนนั้นสามารถทำได้ง่ายๆ
มีราคาสูงกว่าราคาของอุปกรณ์ ใน Android 7.0 (API ระดับ 24) และ
สูงขึ้น ผู้ใช้จึงเปิดใช้การประหยัดอินเทอร์เน็ตได้ในอุปกรณ์ทั้งเครื่องเพื่อ
เพิ่มประสิทธิภาพปริมาณการใช้อินเทอร์เน็ตของอุปกรณ์และลดปริมาณการใช้อินเทอร์เน็ต ความสามารถนี้
มีประโยชน์อย่างยิ่งเมื่อโรมมิ่งเมื่อใกล้สิ้นสุดรอบการเรียกเก็บเงิน
หรือสำหรับแพ็กเกจอินเทอร์เน็ตแบบชำระเงินล่วงหน้าขนาดเล็ก
เมื่อผู้ใช้เปิดใช้โปรแกรมประหยัดอินเทอร์เน็ตในการตั้งค่าและอุปกรณ์
บนเครือข่ายแบบจำกัดปริมาณ ระบบจะบล็อกการใช้อินเทอร์เน็ตที่ใช้งานอยู่เบื้องหลังและสัญญาณ
ให้ใช้อินเทอร์เน็ตน้อยลงในเบื้องหน้าเมื่อเป็นไปได้ ผู้ใช้สามารถ
ทำให้บางแอปสามารถใช้ปริมาณอินเทอร์เน็ตที่วัดเมื่ออยู่เบื้องหลัง แม้ว่าการใช้อินเทอร์เน็ต
โหมดประหยัดเปิดอยู่
Android 7.0 (API ระดับ 24) ได้ขยาย
ConnectivityManager
API เพื่อให้แอปมีวิธีเรียกข้อมูลของผู้ใช้
ค่ากำหนดโหมดประหยัดและค่ากำหนดการตรวจสอบ
การเปลี่ยนแปลง วิธีนี้ถือเป็นแนวทางปฏิบัติที่ดีสำหรับแอปในการตรวจสอบว่า
ผู้ใช้ได้เปิดใช้การประหยัดอินเทอร์เน็ตและพยายามจำกัดการทำงานเบื้องหน้าและ
อินเทอร์เน็ตที่ใช้งานอยู่เบื้องหลัง
ตรวจสอบค่ากำหนดการประหยัดอินเทอร์เน็ต
ใน Android 7.0 (API ระดับ 24) ขึ้นไป แอปสามารถใช้
API ของ ConnectivityManager
เพื่อกำหนดข้อจำกัดการใช้ข้อมูลที่กำลังจะมีผล
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
Intent ที่มี URI ของชื่อแพ็กเกจของแอป ตัวอย่างเช่น
package:MY_APP_ID
การส่ง Intent และ URI จะเป็นการเปิดแอปการตั้งค่า และ
แสดงการตั้งค่าปริมาณการใช้อินเทอร์เน็ตสำหรับแอปของคุณ จากนั้นผู้ใช้ก็จะตัดสินใจได้ว่า
เพื่อเปิดใช้ข้อมูลแบ็กกราวด์สำหรับแอปของคุณ ก่อนที่คุณจะส่ง Intent
แนวทางปฏิบัติที่ดีคือ ถามผู้ใช้ก่อนว่าต้องการเปิดตัว
แอปการตั้งค่าเพื่อจุดประสงค์ในการเปิดใช้ข้อมูลแบ็กกราวด์
ตรวจสอบการเปลี่ยนแปลงของค่ากำหนดการประหยัดอินเทอร์เน็ต
แอปจะตรวจสอบการเปลี่ยนแปลงค่ากำหนดการประหยัดอินเทอร์เน็ตได้โดยการสร้าง
BroadcastReceiver
ถึง
ฟัง ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED
และแบบไดนามิก
การลงทะเบียนตัวรับกับ
Context.registerReceiver()
เมื่อได้รับการออกอากาศนี้ แอปควรตรวจสอบว่าโปรแกรมประหยัดอินเทอร์เน็ตใหม่
ค่ากำหนดมีผลต่อสิทธิ์โดยการเรียกใช้
ConnectivityManager.getRestrictBackgroundStatus()
หมายเหตุ: ระบบจะส่งการประกาศนี้ไปยังแอปที่ลงทะเบียนแบบไดนามิกสำหรับ
ด้วย
Context.registerReceiver()
แอปที่ลงทะเบียนเพื่อรับการแพร่สัญญาณนี้ในไฟล์ Manifest จะไม่ได้รับการแจ้งเตือนดังกล่าว
ทดสอบด้วยคำสั่งของ Android Debug Bridge
Android Debug Bridge (ADB)
ระบุคำสั่ง 2-3 รายการที่คุณใช้ทดสอบแอปในโปรแกรมประหยัดอินเทอร์เน็ตได้
คุณสามารถตรวจสอบและกำหนดค่าเครือข่าย
หรือตั้งค่าเครือข่ายไร้สายเป็นแบบจำกัดปริมาณ เพื่อทดสอบแอปของคุณโดยไม่มีการวัดปริมาณอินเทอร์เน็ต
เครือข่าย
-
$ adb shell dumpsys netpolicy
-
สร้างรายงานที่รวมเครือข่ายพื้นหลังทั่วโลกในปัจจุบัน
การตั้งค่าการจำกัด, UID แพ็กเกจที่ได้รับอนุญาตให้ข้ามโปรแกรมประหยัดอินเทอร์เน็ตในปัจจุบัน และ
สิทธิ์เครือข่ายของแพ็กเกจอื่นๆ ที่รู้จัก
-
$ adb shell cmd netpolicy
-
แสดงรายการคำสั่ง Network Policy Manager (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
-
ตั้งค่า Wi-Fi โดยมี SSID ที่ระบุเป็น "มีการวัดปริมาณอินเทอร์เน็ต" ซึ่งจะช่วยให้คุณจำลอง
เครือข่ายแบบจำกัดปริมาณบนเครือข่ายที่ไม่จำกัดปริมาณอินเทอร์เน็ต
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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."]]