NetworkRequest.Builder


public static class NetworkRequest.Builder
extends Object

java.lang.Object
   ↳ android.net.NetworkRequest.Builder


Builder used to create NetworkRequest objects. Specify the Network features needed in terms of NetworkCapabilities features

Summary

Public constructors

Builder()

Default constructor for Builder.

Builder(NetworkRequest request)

Creates a new Builder of NetworkRequest from an existing instance.

Public methods

NetworkRequest.Builder addCapability(int capability)

Add the given capability requirement to this builder.

NetworkRequest.Builder addTransportType(int transportType)

Adds the given transport requirement to this builder.

NetworkRequest build()

Build NetworkRequest give the current set of capabilities.

NetworkRequest.Builder clearCapabilities()

Completely clears all the NetworkCapabilities from this builder instance, removing even the capabilities that are set by default when the object is constructed.

NetworkRequest.Builder removeCapability(int capability)

Removes (if found) the given capability from this builder instance.

NetworkRequest.Builder removeTransportType(int transportType)

Removes (if found) the given transport from this builder instance.

NetworkRequest.Builder setIncludeOtherUidNetworks(boolean include)

Specifies whether the built request should also match networks that do not apply to the calling UID.

NetworkRequest.Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier)

Sets the optional bearer specific network specifier.

NetworkRequest.Builder setNetworkSpecifier(String networkSpecifier)

This method was deprecated in API level 30. Use setNetworkSpecifier(android.net.NetworkSpecifier) instead.

NetworkRequest.Builder setSubscriptionIds(Set<Integer> subIds)

Sets the optional subscription ID set.

Inherited methods

Public constructors

Builder

Added in API level 21
public Builder ()

Default constructor for Builder.

Builder

Added in API level 31
public Builder (NetworkRequest request)

Creates a new Builder of NetworkRequest from an existing instance.

Parameters
request NetworkRequest: This value cannot be null.

Public methods

addCapability

Added in API level 21
public NetworkRequest.Builder addCapability (int capability)

Add the given capability requirement to this builder. These represent the requested network's required capabilities. Note that when searching for a network to satisfy a request, all capabilities requested must be satisfied.

Parameters
capability int: The capability to add. Value is NetworkCapabilities.NET_CAPABILITY_MMS, NetworkCapabilities.NET_CAPABILITY_SUPL, NetworkCapabilities.NET_CAPABILITY_DUN, NetworkCapabilities.NET_CAPABILITY_FOTA, NetworkCapabilities.NET_CAPABILITY_IMS, NetworkCapabilities.NET_CAPABILITY_CBS, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_IA, NetworkCapabilities.NET_CAPABILITY_RCS, NetworkCapabilities.NET_CAPABILITY_XCAP, NetworkCapabilities.NET_CAPABILITY_EIMS, NetworkCapabilities.NET_CAPABILITY_NOT_METERED, NetworkCapabilities.NET_CAPABILITY_INTERNET, NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED, NetworkCapabilities.NET_CAPABILITY_TRUSTED, NetworkCapabilities.NET_CAPABILITY_NOT_VPN, NetworkCapabilities.NET_CAPABILITY_VALIDATED, NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL, NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, NetworkCapabilities.NET_CAPABILITY_FOREGROUND, NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED, NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, NetworkCapabilities.NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, android.net.NetworkCapabilities.NET_CAPABILITY_VEHICLE_INTERNAL, android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED, NetworkCapabilities.NET_CAPABILITY_ENTERPRISE, android.net.NetworkCapabilities.NET_CAPABILITY_VSIM, android.net.NetworkCapabilities.NET_CAPABILITY_BIP, NetworkCapabilities.NET_CAPABILITY_HEAD_UNIT, NetworkCapabilities.NET_CAPABILITY_MMTEL, NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY, NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH, NetworkCapabilities.NET_CAPABILITY_LOCAL_NETWORK, or NetworkCapabilities.NET_CAPABILITY_NOT_BANDWIDTH_CONSTRAINED

Returns
NetworkRequest.Builder The builder to facilitate chaining builder.addCapability(...).addCapability();.

addTransportType

Added in API level 21
public NetworkRequest.Builder addTransportType (int transportType)

Adds the given transport requirement to this builder. These represent the set of allowed transports for the request. Only networks using one of these transports will satisfy the request. If no particular transports are required, none should be specified here.

Parameters
transportType int: The transport type to add. Value is NetworkCapabilities.TRANSPORT_CELLULAR, NetworkCapabilities.TRANSPORT_WIFI, NetworkCapabilities.TRANSPORT_BLUETOOTH, NetworkCapabilities.TRANSPORT_ETHERNET, NetworkCapabilities.TRANSPORT_VPN, NetworkCapabilities.TRANSPORT_WIFI_AWARE, NetworkCapabilities.TRANSPORT_LOWPAN, android.net.NetworkCapabilities.TRANSPORT_TEST, NetworkCapabilities.TRANSPORT_USB, NetworkCapabilities.TRANSPORT_THREAD, or NetworkCapabilities.TRANSPORT_SATELLITE

Returns
NetworkRequest.Builder The builder to facilitate chaining.

build

Added in API level 21
public NetworkRequest build ()

Build NetworkRequest give the current set of capabilities.

Returns
NetworkRequest

clearCapabilities

Added in API level 30
public NetworkRequest.Builder clearCapabilities ()

Completely clears all the NetworkCapabilities from this builder instance, removing even the capabilities that are set by default when the object is constructed. Also removes any set forbidden capabilities.

Returns
NetworkRequest.Builder The builder to facilitate chaining. This value cannot be null.

removeCapability

Added in API level 21
public NetworkRequest.Builder removeCapability (int capability)

Removes (if found) the given capability from this builder instance.

Parameters
capability int: The capability to remove. Value is NetworkCapabilities.NET_CAPABILITY_MMS, NetworkCapabilities.NET_CAPABILITY_SUPL, NetworkCapabilities.NET_CAPABILITY_DUN, NetworkCapabilities.NET_CAPABILITY_FOTA, NetworkCapabilities.NET_CAPABILITY_IMS, NetworkCapabilities.NET_CAPABILITY_CBS, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_IA, NetworkCapabilities.NET_CAPABILITY_RCS, NetworkCapabilities.NET_CAPABILITY_XCAP, NetworkCapabilities.NET_CAPABILITY_EIMS, NetworkCapabilities.NET_CAPABILITY_NOT_METERED, NetworkCapabilities.NET_CAPABILITY_INTERNET, NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED, NetworkCapabilities.NET_CAPABILITY_TRUSTED, NetworkCapabilities.NET_CAPABILITY_NOT_VPN, NetworkCapabilities.NET_CAPABILITY_VALIDATED, NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL, NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, NetworkCapabilities.NET_CAPABILITY_FOREGROUND, NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED, NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, NetworkCapabilities.NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, android.net.NetworkCapabilities.NET_CAPABILITY_VEHICLE_INTERNAL, android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED, NetworkCapabilities.NET_CAPABILITY_ENTERPRISE, android.net.NetworkCapabilities.NET_CAPABILITY_VSIM, android.net.NetworkCapabilities.NET_CAPABILITY_BIP, NetworkCapabilities.NET_CAPABILITY_HEAD_UNIT, NetworkCapabilities.NET_CAPABILITY_MMTEL, NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY, NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH, NetworkCapabilities.NET_CAPABILITY_LOCAL_NETWORK, or NetworkCapabilities.NET_CAPABILITY_NOT_BANDWIDTH_CONSTRAINED

Returns
NetworkRequest.Builder The builder to facilitate chaining.

removeTransportType

Added in API level 21
public NetworkRequest.Builder removeTransportType (int transportType)

Removes (if found) the given transport from this builder instance.

Parameters
transportType int: The transport type to remove. Value is NetworkCapabilities.TRANSPORT_CELLULAR, NetworkCapabilities.TRANSPORT_WIFI, NetworkCapabilities.TRANSPORT_BLUETOOTH, NetworkCapabilities.TRANSPORT_ETHERNET, NetworkCapabilities.TRANSPORT_VPN, NetworkCapabilities.TRANSPORT_WIFI_AWARE, NetworkCapabilities.TRANSPORT_LOWPAN, android.net.NetworkCapabilities.TRANSPORT_TEST, NetworkCapabilities.TRANSPORT_USB, NetworkCapabilities.TRANSPORT_THREAD, or NetworkCapabilities.TRANSPORT_SATELLITE

Returns
NetworkRequest.Builder The builder to facilitate chaining.

setIncludeOtherUidNetworks

Added in API level 31
public NetworkRequest.Builder setIncludeOtherUidNetworks (boolean include)

Specifies whether the built request should also match networks that do not apply to the calling UID. By default, the built request will only match networks that apply to the calling UID. If this method is called with true, the built request will match any network on the system that matches the other parameters of the request. In this case, any information in the built request that is subject to redaction for security or privacy purposes, such as a NetworkSpecifier, will be redacted or removed to prevent the application deducing sensitive information.

Parameters
include boolean: Whether to match networks that do not apply to the calling UID.

Returns
NetworkRequest.Builder The builder to facilitate chaining. This value cannot be null.

setNetworkSpecifier

Added in API level 26
public NetworkRequest.Builder setNetworkSpecifier (NetworkSpecifier networkSpecifier)

Sets the optional bearer specific network specifier. This has no meaning if a single transport is also not specified, so calling this without a single transport set will generate an exception, as will subsequently adding or removing transports after this is set.

Parameters
networkSpecifier NetworkSpecifier: A concrete, parcelable framework class that extends NetworkSpecifier.

Returns
NetworkRequest.Builder

setNetworkSpecifier

Added in API level 21
Deprecated in API level 30
public NetworkRequest.Builder setNetworkSpecifier (String networkSpecifier)

This method was deprecated in API level 30.
Use setNetworkSpecifier(android.net.NetworkSpecifier) instead.

Sets the optional bearer specific network specifier. This has no meaning if a single transport is also not specified, so calling this without a single transport set will generate an exception, as will subsequently adding or removing transports after this is set.

If the networkSpecifier is provided, it shall be interpreted as follows:
  • If the specifier can be parsed as an integer, it will be treated as a TelephonyNetworkSpecifier, and the provided integer will be interpreted as a SubscriptionId.
  • If the value is an ethernet interface name, it will be treated as such.
  • For all other cases, the behavior is undefined.

Parameters
networkSpecifier String: A String of either a SubscriptionId in cellular network request or an ethernet interface name in ethernet network request.

Returns
NetworkRequest.Builder

setSubscriptionIds

Added in API level 35
Also in U Extensions 12
public NetworkRequest.Builder setSubscriptionIds (Set<Integer> subIds)

Sets the optional subscription ID set.

This specify the subscription IDs requirement. A network will satisfy this request only if it matches one of the subIds in this set. An empty set matches all networks, including those without a subId.

Registering a NetworkRequest with a non-empty set of subIds requires the NETWORK_FACTORY permission.

Parameters
subIds Set: A Set that represents subscription IDs. This value cannot be null.

Returns
NetworkRequest.Builder This value cannot be null.