I'm trying to understand the exact behavior of ConnectivityManager.getActiveNetwork() in the context of Per-Application Network Selection (PANS) in Android Automotive.
The official documentation says: https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetwork()
"Returns a Network object corresponding to the currently active default data network. This will return null when there is no default network, or when the default network is blocked."
From this wording, it sounds like getActiveNetwork() always returns the system-wide default network — the one that all apps use unless they've been explicitly bound or routed differently.
However, in Android Automotive, apps can be routed to use specific network types like OEM_PAID or OEM_PRIVATE via PANS, based on network preference settings. In that case:
In this case — where an app is using a different network due to a PANS preference — 👉 Does getActiveNetwork() return the network actually routing traffic for the current app, or just the system-wide default?
Additional Notes:
Thanks in advance for any help or references!
In this case — where an app is using a different network due to a PANS preference — 👉 Does getActiveNetwork() return the network actually routing traffic for the current app, or just the system-wide default?
It returns the active (default) network for the app, not the system default.
If this behavior changed in a particular Android version (e.g., Android 11+), I’d really appreciate clarification.
PANS officially landed in Android 12, however it is always possible that an OEM could backport this feature to older releases if they chose to.
If there is any official Android documentation or source code reference confirming how this behaves under PANS, that would be very helpful.
Official documentation on the PANS feature can be found at https://source.android.com/docs/automotive/connectivity. The pertinent text in the docs for this question would be "Network preferences are used to set the default network of an app."
"Default" and "active" network are used interchangeably (with default being much more widely used these days). Given this, if the app's default network changed, but getActiveNetwork()
returned the system default, it would break the intended functionality for that API (as well as the behavior of all the apps that depend on it).
The code for getActiveNetowrk
proves this:
@Override
@Nullable
public Network getActiveNetwork() {
enforceAccessPermission();
return getActiveNetworkForUidInternal(mDeps.getCallingUid(), false);
}
getActiveNetworkForUidInternal
will, as it is named, get the active network for the UID (app), as opposed to the system default (you can step through the code if you are interested).