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.Builder
|
clearCapabilities()
Completely clears all the |
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 |
NetworkRequest.Builder
|
setSubscriptionIds(Set<Integer> subIds)
Sets the optional subscription ID set. |
Inherited methods | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Builder
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
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.
Returns | |
---|---|
NetworkRequest.Builder |
The builder to facilitate chaining
builder.addCapability(...).addCapability(); . |
addTransportType
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
public NetworkRequest build ()
Build NetworkRequest
give the current set of capabilities.
Returns | |
---|---|
NetworkRequest |
clearCapabilities
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
public NetworkRequest.Builder removeCapability (int capability)
Removes (if found) the given capability from this builder instance.
Returns | |
---|---|
NetworkRequest.Builder |
The builder to facilitate chaining. |
removeTransportType
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
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
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
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 thenetworkSpecifier
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
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 . |