自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
按应用选择网络 (PANS)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
随着 OEM 和车辆所有者申请的用例日益增多,汽车越来越依赖网络连接来管理这些用例,从而导致流量使用和相关费用也相应增加。使用按应用选择网络 (PANS) 功能可以在 OEM 付费的网络中发送指定应用的流量。
借助 PANS,您可以管理流量用量和费用,同时提供安全可靠的互联车载体验。PANS:
- 包含一个添加到
ConnectivityManager
中的新 API,仅供汽车设备使用。
- 提供更新后的 WLAN 建议 API(请参阅适用于互联网连接的 WLAN 建议 API),用于支持动态更改的 PANS 网络功能。
- 收集辅助指标。
- 提供参考应用。
为什么使用 PANS?
PANS 具有以下特点:
- 动态更新应用到网络的映射。
- 无需对应用进行任何更改即可管理应用级路由。
- 只有 OEM 允许的应用才能访问映射的 OEM 网络。
- 应用开发者无需进行任何更改即可实现此功能。
- 面向用户的指标会针对 OEM 管理的网络跟踪应用到网络的流量使用情况。
- 网络访问十分安全,不会被意外用例或未经授权的应用滥用。
- PANS 应用到网络映射的更改会传达给用户。
- 对所有用户应用相同的网络配置。
核心优势
PANS 可为 OEM 提供以下核心优势:
- OEM 可以代用户支付网络流量的费用:
- 用户可以免费获得系统更新。
- 指定应用使用网络无需用户付费。
- 遥测和其他分析的管理不会向用户收费。
- 即使用户未付费购买流量套餐,OEM 也可以确保关键应用保持网络连接。例如,即使用户没有流量套餐,地图、智能助理(免触摸驾驶)和系统更新等对保障安全至关重要的功能也会继续运行。
- PANS 可以专门针对 Android 中的网络流量路由提供更加精细的控制。例如,OEM 可以为应用级流量的发送定义最适宜的逻辑网络拓扑。

图 1. PANS 框架
实现 PANS
为了实现 PANS,新提供了一个 ConnectivityManager
API setOemNetworkPreference
。这一新 API 可将应用映射到一个 OemNetworkPreference
。此 API 仅供汽车设备使用,并带有 @SystemApi
注解,具有新的 signature
权限。

图 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 网络(如果应用选择这样做)。
查看参考应用
user-debug Automotive build 中提供了一个名为 NetworkPreferenceApp
的参考应用(包括代码),并演示了如何执行以下操作:
- 使用 PANS 指标。
- 设置 PANS 政策。
- 为设备设置默认政策。
- 清除政策。
- 在启动时应用政策。
- 使用防止驾驶员分心 API(请参阅有关防止驾驶员分心的准则)。
- 使用
OEM_PAID
和 OEM_PRIVATE
动态更新 Wi-Fi。

图 3. 参考应用
指标
为了提高流量使用的透明度,系统会就通过 OEM_PAID
和 OEM_PRIVATE
网络映射传输的数据量收集并提供相关指标。
问题排查
大多数问题排查情况的起因都是应用使用了错误的网络(无网络连接)或超额使用流量。为了快速地解决问题:
- 连接
dumpsys
包含一个列表,列出了按应用分配的有效默认网络及其关联的应用(通过 PANS 映射)。
- netd
dumpsys
包含 UID IP 和防火墙规则。
- netstats
dumpsys
包含 PANS 按应用的指标。例如,哪些应用使用了哪个 OEM 网络。
创建一个 Android bug 报告即可获得所有 dumpsys
数据。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]