Join us for ⁠#Android11: The Beta Launch Show on June 3!
Added in API level 21

Builder

open class Builder
kotlin.Any
   ↳ android.net.NetworkRequest.Builder

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

Summary

Public constructors

Default constructor for Builder.

Public methods

open NetworkRequest.Builder!
addCapability(capability: Int)

Add the given capability requirement to this builder.

open NetworkRequest.Builder!
addTransportType(transportType: Int)

Adds the given transport requirement to this builder.

open NetworkRequest!

Build NetworkRequest give the current set of capabilities.

open NetworkRequest.Builder

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

open NetworkRequest.Builder!
removeCapability(capability: Int)

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

open NetworkRequest.Builder!
removeTransportType(transportType: Int)

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

open NetworkRequest.Builder!
setNetworkSpecifier(networkSpecifier: String!)

Sets the optional bearer specific network specifier.

open NetworkRequest.Builder!

Sets the optional bearer specific network specifier.

Public constructors

<init>

Added in API level 21
Builder()

Default constructor for Builder.

Public methods

addCapability

Added in API level 21
open fun addCapability(capability: Int): NetworkRequest.Builder!

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 android.net.NetworkCapabilities#NET_CAPABILITY_MMS, android.net.NetworkCapabilities#NET_CAPABILITY_SUPL, android.net.NetworkCapabilities#NET_CAPABILITY_DUN, android.net.NetworkCapabilities#NET_CAPABILITY_FOTA, android.net.NetworkCapabilities#NET_CAPABILITY_IMS, android.net.NetworkCapabilities#NET_CAPABILITY_CBS, android.net.NetworkCapabilities#NET_CAPABILITY_WIFI_P2P, android.net.NetworkCapabilities#NET_CAPABILITY_IA, android.net.NetworkCapabilities#NET_CAPABILITY_RCS, android.net.NetworkCapabilities#NET_CAPABILITY_XCAP, android.net.NetworkCapabilities#NET_CAPABILITY_EIMS, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_METERED, android.net.NetworkCapabilities#NET_CAPABILITY_INTERNET, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_RESTRICTED, android.net.NetworkCapabilities#NET_CAPABILITY_TRUSTED, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_VPN, android.net.NetworkCapabilities#NET_CAPABILITY_VALIDATED, android.net.NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_ROAMING, android.net.NetworkCapabilities#NET_CAPABILITY_FOREGROUND, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_CONGESTED, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, android.net.NetworkCapabilities#NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, or android.net.NetworkCapabilities#NET_CAPABILITY_TEMPORARILY_NOT_METERED
Return
NetworkRequest.Builder! The builder to facilitate chaining builder.addCapability(...).addCapability();.

addTransportType

Added in API level 21
open fun addTransportType(transportType: Int): NetworkRequest.Builder!

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 android.net.NetworkCapabilities#TRANSPORT_CELLULAR, android.net.NetworkCapabilities#TRANSPORT_WIFI, android.net.NetworkCapabilities#TRANSPORT_BLUETOOTH, android.net.NetworkCapabilities#TRANSPORT_ETHERNET, android.net.NetworkCapabilities#TRANSPORT_VPN, android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE, android.net.NetworkCapabilities#TRANSPORT_LOWPAN, or android.net.NetworkCapabilities.TRANSPORT_TEST
Return
NetworkRequest.Builder! The builder to facilitate chaining.

build

Added in API level 21
open fun build(): NetworkRequest!

Build NetworkRequest give the current set of capabilities.

clearCapabilities

Added in API level R
open fun clearCapabilities(): NetworkRequest.Builder

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

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

removeCapability

Added in API level 21
open fun removeCapability(capability: Int): NetworkRequest.Builder!

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

Parameters
capability Int: The capability to remove. Value is android.net.NetworkCapabilities#NET_CAPABILITY_MMS, android.net.NetworkCapabilities#NET_CAPABILITY_SUPL, android.net.NetworkCapabilities#NET_CAPABILITY_DUN, android.net.NetworkCapabilities#NET_CAPABILITY_FOTA, android.net.NetworkCapabilities#NET_CAPABILITY_IMS, android.net.NetworkCapabilities#NET_CAPABILITY_CBS, android.net.NetworkCapabilities#NET_CAPABILITY_WIFI_P2P, android.net.NetworkCapabilities#NET_CAPABILITY_IA, android.net.NetworkCapabilities#NET_CAPABILITY_RCS, android.net.NetworkCapabilities#NET_CAPABILITY_XCAP, android.net.NetworkCapabilities#NET_CAPABILITY_EIMS, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_METERED, android.net.NetworkCapabilities#NET_CAPABILITY_INTERNET, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_RESTRICTED, android.net.NetworkCapabilities#NET_CAPABILITY_TRUSTED, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_VPN, android.net.NetworkCapabilities#NET_CAPABILITY_VALIDATED, android.net.NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_ROAMING, android.net.NetworkCapabilities#NET_CAPABILITY_FOREGROUND, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_CONGESTED, android.net.NetworkCapabilities#NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, android.net.NetworkCapabilities#NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, or android.net.NetworkCapabilities#NET_CAPABILITY_TEMPORARILY_NOT_METERED
Return
NetworkRequest.Builder! The builder to facilitate chaining.

removeTransportType

Added in API level 21
open fun removeTransportType(transportType: Int): NetworkRequest.Builder!

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

Parameters
transportType Int: The transport type to remove. Value is android.net.NetworkCapabilities#TRANSPORT_CELLULAR, android.net.NetworkCapabilities#TRANSPORT_WIFI, android.net.NetworkCapabilities#TRANSPORT_BLUETOOTH, android.net.NetworkCapabilities#TRANSPORT_ETHERNET, android.net.NetworkCapabilities#TRANSPORT_VPN, android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE, android.net.NetworkCapabilities#TRANSPORT_LOWPAN, or android.net.NetworkCapabilities.TRANSPORT_TEST
Return
NetworkRequest.Builder! The builder to facilitate chaining.

setNetworkSpecifier

Added in API level 21
Deprecated in API level R
open fun setNetworkSpecifier(networkSpecifier: String!): NetworkRequest.Builder!

Deprecated: 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.

setNetworkSpecifier

Added in API level 26
open fun setNetworkSpecifier(networkSpecifier: NetworkSpecifier!): NetworkRequest.Builder!

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.