自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
個別應用程式網路選取 (PANS)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
車輛越來越仰賴連線功能,才能管理原始設備製造商和車輛車主要求的用途,導致資料足跡和相關成本增加。使用個別應用程式網路選取 (PANS) 功能,將指定應用程式的流量路由至 OEM 付費的網路。
有了 PANS,您就能管理資料用量量和費用,同時提供穩健、安全的車輛連網體驗。PANS:
- 包含新增至
ConnectivityManager
的新 API,僅適用於汽車裝置。
- 提供更新版的 Wi-Fi 建議 API (請參閱「用於網際網路連線的 Wi-Fi 建議 API」),以便支援動態變更 PANS 網路功能。
- 收集相關指標。
- 提供參考應用程式。
為什麼要使用 PANS?
PANS 可執行以下操作:
- 動態更新應用程式與網路的對應項目。
- 不必變更應用程式,即可管理應用程式層級的路由。
- 只有原始設備製造商 (OEM) 允許的應用程式才能存取對應的 OEM 網路。
- 應用程式開發人員無須進行任何變更即可實作這項功能。
- 使用者面指標會追蹤 OEM 管理網路的應用程式到網路資料用量。
- 網路存取權是安全的,不會因意外用途或未經授權的應用程式而遭到濫用。
- 向使用者說明 PANS 應用程式至網路對應項目的異動。
- 所有使用者都會套用相同的網路設定。
核心優勢
PANS 為原始設備製造商 (OEM) 提供以下核心優勢:
- OEM 可以為網路流量付費,而非使用者:
- 可免費為使用者提供系統更新。
- 可免費為使用者提供指定應用程式的網路用量。
- 使用者可以免費管理遙測資料和其他分析資料。
- 即使沒有使用者付費資料方案,原始設備製造商 (OEM) 也能確保重要應用程式保持連線。舉例來說,即使使用者沒有數據方案,安全性至關重要的功能 (例如地圖、Google 助理 (免手持駕駛) 和系統更新) 仍會繼續運作。
- PANS 提供精細的控管功能,可針對 Android 中的網路流量路由進行控管。舉例來說,原始設備製造商可以為應用程式層級流量路由最佳化定義邏輯網路拓撲。

圖 1. PANS 架構
實作 PANS
為了實作 PANS,我們提供新的 ConnectivityManager
API setOemNetworkPreference
。這個新 API 會將應用程式對應至 OemNetworkPreference
。這個 API 僅適用於汽車裝置,並會標註為具有新 signature
權限的 @SystemApi
。

圖 2. 實作 PANS
OemNetworkPreference
OemNetworkPreference
是 OEM_PAID
和 OEM_PRIVATE
NetworkCapabilities
的抽象概念,可將應用程式依套件名稱對應至網路偏好設定。網路偏好設定可讓網路建立階層。舉例來說,將應用程式對應至 OEM_NETWORK_PREFERENCE_OEM_PAID
偏好設定,會導致系統為應用程式指派的預設網路,依下列優先順序:首先使用 UNMETERED
網路,如果 UNMETERED
無法使用,則使用 OEM_PAID
網路,如果 OEM_PAID
無法使用,則使用系統預設網路。
OEM_PAID
主要用於可在 OEM 和非 OEM 網路上進行路由的應用程式。
OEM_PRIVATE
主要用於 OEM 應用程式,讓應用程式存取專屬網路。
/**
* If an unmetered network is available, use it.
* Otherwise, if a network with the OEM_PAID capability is available, use it.
* Otherwise, use the general default network.
*/
public static final int OEM_NETWORK_PREFERENCE_OEM_PAID = 1;
/**
* If an unmetered network is available, use it.
* Otherwise, if a network with the OEM_PAID capability is available, use it.
* Otherwise, the app doesn't get a default network.
*/
public static final int OEM_NETWORK_PREFERENCE_OEM_PAID_NO_FALLBACK = 2;
/**
* Use only NET_CAPABILITY_OEM_PAID networks.
*/
public static final int OEM_NETWORK_PREFERENCE_OEM_PAID_ONLY = 3;
/**
* Use only NET_CAPABILITY_OEM_PRIVATE networks.
*/
public static final int OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY = 4;
呼叫 PANS API
如何使用 PANS API:
- 使用
OemNetworkPreferences
將應用程式對應至網路偏好設定。
- 使用
OemNetworkPreferences
物件呼叫 setOemNetworkPreference
。
- 使用
Runnable
介面監聽 API 完成情形。
例如:
// Mapping three packages to two network preferences
// Packages have a 1:1 mapping to network preferences
OemNetworkPreferences pref = new OemNetworkPreferences.Builder()
.addNetworkPreference("first.package.name", OEM_NETWORK_PREFERENCE_OEM_PAID)
.addNetworkPreference("second.package.name", OEM_NETWORK_PREFERENCE_OEM_PAID)
.addNetworkPreference("third.package.name", OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY)
.build();
myConnectivityManager.setOemNetworkPreference(pref, myExecutor, myListener);
注意事項
實作 PANS 時,請注意下列事項:
- 網路偏好設定不會在開機後保留,每次開機時都需要重新套用。
- 如要為應用程式建立偏好設定,應用程式不必安裝。因此,您可以主動設定已解除安裝的應用程式的網路偏好設定。
- 應用程式只能對應至單一網路偏好設定。
- 網路偏好設定可用於設定應用程式的預設網路。如果應用程式未透過某個專用 API 指定要使用的網路,系統就會使用這項網路。這不僅涵蓋絕大多數的連線需求,還可讓您繼續使用專用 API (例如
NetworkRequest
API),以免中斷現有應用程式用途。舉例來說,如果應用程式只想透過無限量網路執行作業,PANS 就不會強制使用其他網路。
使用對應的網路偏好設定時,必須有具備 OEM_PAID
或 OEM_PRIVATE
功能的網路。Android 支援乙太網路和 Wi-Fi 網路的功能設定。針對乙太網路,您可以使用資源疊加層 config_ethernet_interfaces
。這會在編譯期間設定。
針對 Wi-Fi,WifiNetworkSuggestion
API 可搭配新的 Android 12 API setOemPaid(Boolean)
和 setOemPrivate(Boolean)
使用。您可以在執行階段變更這項設定。
請參考以下範例:
- 名為
config_ethernet_interfaces
的資源疊加層指定:
- 要設定的介面名稱。
- 所需的
NetworkCapabilities
值。<!-- 11 NET_CAPABILITY_NOT_METERED
12 NET_CAPABILITY_INTERNET
14 NET_CAPABILITY_TRUSTED
15 NET_CAPABILITY_NOT_VPN
22 NET_CAPABILITY_OEM_PAID || 26 NET_CAPABILITY_OEM_PRIVATE -->
<string-array translatable="false" name="config_ethernet_interfaces">
<item>eth0;11,12,14,15,22;;</item></string-array>
- 這個
WiFiNetworkSuggestion
可以動態變更:ArrayList<WifiNetworkSuggestion> list = new ArrayList<>();
list.add(new WifiNetworkSuggestion.Builder()
.setSsid(WifiInfo.sanitizeSsid(ssid))
.setOemPrivate(true)
.build());
mWifiManager.addNetworkSuggestions(list);
限制 PANS 網路的存取權
使用 OEM_PAID
或 OEM_PRIVATE
功能標記網路,即可將該網路標示為受管制網路。受限網路只能由具備 CONNECTIVITY_USE_RESTRICTED_NETWORKS
權限的應用程式使用,這項權限由 OEM 廠商控管。
具備這項權限的應用程式可以使用受限制的網路,前提是應用程式明確要求。不過,這些應用程式不會預設為受限制的網路。透過 PANS 對應的應用程式可以將受限制的 OEM 網路設為預設值,且不需要受限制的網路權限即可使用。如果這類應用程式有由 PANS 指派的受限制 OEM 網路,並將其設為預設網路,則應用程式也可以選擇明確要求該 OEM 網路。
查看參考應用程式
在使用者偵錯汽車版本中提供名為 NetworkPreferenceApp
的參考應用程式 (包括程式碼),並示範如何執行下列操作:
- 使用 PANS 指標。
- 設定 PANS 政策。
- 為裝置設定預設政策。
- 清除政策。
- 在啟動時套用政策。
- 使用 Driver Distraction API (請參閱「駕駛人分心指南」)。
- 使用
OEM_PAID
和 OEM_PRIVATE
動態更新 Wi-Fi。

圖 3. 參考應用程式
指標
為讓資料使用情形更透明,系統會收集並提供透過 OEM_PAID
和 OEM_PRIVATE
網路對應項目傳輸的資料量相關指標。
疑難排解
大多數的疑難排解條件都是因為應用程式使用錯誤的網路 (沒有網路連線) 或資料超出。如要啟用快速解決方案,請按照下列步驟操作:
- 連線
dumpsys
包含每個應用程式預設網路和相關應用程式 (從 PANS 對應) 的清單。
- Netd
dumpsys
包含 UID IP 和防火牆規則。
- Netstats
dumpsys
包含每個應用程式的 PANS 指標。例如,哪些應用程式使用哪些 OEM 網路。
您可以建立 Android 錯誤報告,取得所有 dumpsys
資料。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-06-12 (世界標準時間)。
[[["容易理解","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-06-12 (世界標準時間)。"],[],[],null,["# Per-application network selection (PANS)\n\nVehicles increasingly rely on connectivity to manage the growing list of\nuse cases requested by both OEMs and vehicle owners, resulting in an increased\ndata footprint and associated costs. Use the *per-application network selection\n(PANS)* feature to route the traffic of specified apps on networks paid for\nby the OEM.\n\nWith PANS, you can manage the volume and cost of data usage while simultaneously\nproviding a robust, secure, and connected car experience. PANS:\n\n- Consists of a new API added to `ConnectivityManager` available only to automotive devices.\n- Provides an updated Wi-Fi suggestion API (see [Wi-Fi suggestion API for internet connectivity](https://developer.android.com/guide/topics/connectivity/wifi-suggest)) to include support for dynamically changing PANS network capabilities.\n- Collects supporting metrics.\n- Provides a reference app.\n\nWhy PANS?\n---------\n\nPANS can:\n\n- Dynamically update app-to-network mappings.\n- Manage app-level routing without making changes to apps.\n- Only OEM-allowed apps can access the mapped OEM networks.\n- App developers needn't make any changes to implement this feature.\n- User-facing metrics track app-to-network data usage for OEM managed networks.\n- Network access is secure and can't be abused through unintended use cases or unauthorized apps.\n- Changes to PANS app-to-network mappings are communicated to users.\n- The same network configuration is applied across all users.\n\n### Core advantages\n\nPANS provides OEMS with these core advantages:\n\n1. OEMs can pay for network traffic instead of users:\n - System updates can be provided at no cost to the user.\n - Network usage of specified apps can be provided at no cost to the user.\n - Telemetry and other analytics can be managed at no cost to the user.\n2. OEMs can ensure critical apps remain connected even without a user-paid data plan. For example, safety-critical features such as maps, assistant (hands-free driving), and system updates continue to function even when a user has no data plan.\n3. PANS provides additional granularity of control specific to network traffic routing in Android. For example, OEMs can optimally define a logical network topology for the routing of app-level traffic.\n\n**Figure 1**. PANS framework\n\nImplement PANS\n--------------\n\nTo implement PANS, a new `ConnectivityManager` API,\n`setOemNetworkPreference`, is provided.\nThis new API maps apps to an `OemNetworkPreference`.\nThis API is available only to automotive devices and is annotated as a\n`@SystemApi` with a new `signature` permission.\n\n**Figure 2.** Implement PANS\n\n### OemNetworkPreference\n\n`OemNetworkPreference` is an abstraction over `OEM_PAID` and\n`OEM_PRIVATE` `NetworkCapabilities` mapping apps by package name\nto a *network preference* . Network preferences allow for network hierarchies.\nFor example, mapping an app to the `OEM_NETWORK_PREFERENCE_OEM_PAID` preference\nresults in the following priority of default networks assigned to an app: use an\n`UNMETERED` network first, if `UNMETERED` is not available use an\n`OEM_PAID` network, and if `OEM_PAID` is not available, use the system\ndefault network.\n\n- `OEM_PAID` Used primarily for apps that can be routed on both OEM and non-OEM networks.\n- `OEM_PRIVATE` Used primarily for OEM apps to gain access to a network dedicated to them.\n\n```scilab\n/**\n* If an unmetered network is available, use it.\n* Otherwise, if a network with the OEM_PAID capability is available, use it.\n* Otherwise, use the general default network.\n*/\npublic static final int OEM_NETWORK_PREFERENCE_OEM_PAID = 1;\n\n/**\n* If an unmetered network is available, use it.\n* Otherwise, if a network with the OEM_PAID capability is available, use it.\n* Otherwise, the app doesn't get a default network.\n*/\npublic static final int OEM_NETWORK_PREFERENCE_OEM_PAID_NO_FALLBACK = 2;\n\n/**\n* Use only NET_CAPABILITY_OEM_PAID networks.\n*/\npublic static final int OEM_NETWORK_PREFERENCE_OEM_PAID_ONLY = 3;\n\n/**\n* Use only NET_CAPABILITY_OEM_PRIVATE networks.\n*/\npublic static final int OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY = 4;\n```\n\n### Call PANS APIs\n\nTo use PANS APIs:\n\n1. Use `OemNetworkPreferences` to map an app to a network preference.\n2. Call `setOemNetworkPreference` with the `OemNetworkPreferences` object.\n3. Use the `Runnable` interface to listen for API completion.\n\nFor example: \n\n```carbon\n// Mapping three packages to two network preferences\n// Packages have a 1:1 mapping to network preferences\nOemNetworkPreferences pref = new OemNetworkPreferences.Builder()\n .addNetworkPreference(\"first.package.name\", OEM_NETWORK_PREFERENCE_OEM_PAID)\n .addNetworkPreference(\"second.package.name\", OEM_NETWORK_PREFERENCE_OEM_PAID)\n .addNetworkPreference(\"third.package.name\", OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY)\n .build();\n\nmyConnectivityManager.setOemNetworkPreference(pref, myExecutor, myListener);\n```\n\n### Considerations\n\nAs you implement PANS, keep the following points in mind:\n\n- Network preferences aren't persisted across boots and needs to be reapplied on each boot.\n- To create a preference for an app, it needn't be installed. Therefore, network preferences for uninstalled apps can be set proactively.\n- At any given time, an app can only be mapped to a single network preference.\n- Network preferences are used to set the default network of an app. This is the network used when an app hasn't specified what network(s) it wants to use through one of the specialized APIs. Not only does this cover the vast majority of connectivity needs, it also allows continued usage of specialized APIs such as the `NetworkRequest` API so as to not break existing app use-cases. For example, when an app only wants to do an operation over an unmetered network, PANS won't force it to use another network.\n\nConfigure a network\n-------------------\n\nA network with either the `OEM_PAID` or `OEM_PRIVATE`\ncapabilities must be available when using a corresponding network preference. Android\nprovides support for the configuration of capabilities for Ethernet and Wi-Fi networks. For\nEthernet networks, you can use a resource overlay, `config_ethernet_interfaces`.\nThis is set at compile time.\n\nFor Wi-Fi, the `WifiNetworkSuggestion` API can be used with the new\nAndroid 12 APIs, `setOemPaid(Boolean)` and\n`setOemPrivate(Boolean)`. This can be changed at runtime.\n\nConsider these examples:\n\n1. A resource overlay named `config_ethernet_interfaces` specifies:\n - The name of the interface to configure.\n - The desired `NetworkCapabilities` values. \n\n ```scdoc\n \u003c!-- 11 NET_CAPABILITY_NOT_METERED\n 12 NET_CAPABILITY_INTERNET\n 14 NET_CAPABILITY_TRUSTED\n 15 NET_CAPABILITY_NOT_VPN\n 22 NET_CAPABILITY_OEM_PAID || 26 NET_CAPABILITY_OEM_PRIVATE --\u003e\n \u003cstring-array translatable=\"false\" name=\"config_ethernet_interfaces\"\u003e\n \u003citem\u003eeth0;11,12,14,15,22;;\u003c/item\u003e\u003c/string-array\u003e\n ```\n2. This `WiFiNetworkSuggestion` can be changed dynamically: \n\n ```text\n ArrayList\u003cWifiNetworkSuggestion\u003e list = new ArrayList\u003c\u003e();\n list.add(new WifiNetworkSuggestion.Builder()\n .setSsid(WifiInfo.sanitizeSsid(ssid))\n .setOemPrivate(true)\n .build());\n mWifiManager.addNetworkSuggestions(list);\n ```\n\nRestrict access to PANS networks\n--------------------------------\n\nTagging a network with either the `OEM_PAID` or `OEM_PRIVATE`\ncapabilities marks that network as a *restricted network* . Restricted networks\ncan *only* be used by apps that have the\n`CONNECTIVITY_USE_RESTRICTED_NETWORKS` permission, which is controlled by OEMs.\n\nApps with this permission can use restricted networks *provided the apps explicitly request\nthem*. However, these apps won't get restricted networks as their default. Apps\nmapped through PANS can have restricted OEM networks set as their default and won't need\nthe restricted network permission to use them. When such an app has a restricted\nOEM network assigned as its default network by PANS, it also has the ability to\nexplicitly request said OEM network if the app chooses to do so.\n\nReview the reference app\n------------------------\n\nA reference app (including code) named `NetworkPreferenceApp` is provided\nin user-debug automotive builds and demonstrates how to:\n\n- Consume PANS metrics.\n- Set PANS policy.\n- Set a default policy for the device.\n- Clear a policy.\n- Apply a policy on boot.\n- Use the Driver Distraction API (see [Driver Distraction Guidelines](/docs/automotive/driver_distraction/guidelines)).\n- Dynamically update Wi-Fi with `OEM_PAID` and `OEM_PRIVATE`.\n\n**Figure 3.** Reference app\n\nMetrics\n-------\n\nTo facilitate transparency around data usage, metrics are collected and made\navailable regarding the amount of data transmitted over the\n`OEM_PAID` and `OEM_PRIVATE` network mappings.\n\nTroubleshooting\n---------------\n\nMost troubleshooting conditions arise from either an app using the wrong network\n(no network connectivity) or data overages. To enable quick resolutions:\n\n- Connectivity `dumpsys` includes a list of active per-app default networks and their associated apps (mapped from PANS).\n- Netd `dumpsys` includes UID IP and firewall rules.\n- Netstats `dumpsys` includes PANS per-app metrics. For example, which apps used which OEM network.\n\nAll `dumpsys` data is available by creating an Android bugreport."]]