NetworkRequest

public class NetworkRequest
extends Object implements Parcelable

java.lang.Object
   ↳ android.net.NetworkRequest


An object describing a network that the application is interested in.

@see NetworkRequest.Builder and used to request a network via ConnectivityManager#requestNetwork or to listen for changes via the ConnectivityManager#registerNetworkCallback family of functions.

ConnectivityManager#requestNetwork will try to find a connected network matching the NetworkRequest, and return it if there is one. As long as the request is outstanding, the system will try to find the best possible network that matches the request. The request will keep up the currently best connected network, and if a better one is found (e.g. cheaper or faster) the system will bring up that better network to better serve the request. A request filed with ConnectivityManager#requestNetwork will only match one network at a time (the one the system thinks is best), even if other networks can match the request that are being kept up by other requests. For example, an application needing a network with NetworkCapabilities#NET_CAPABILITY_INTERNET should use ConnectivityManager#requestNetwork to request the system keeps one up. A general cellular network can satisfy this request, but if the system finds a free Wi-Fi network which is expected to be faster, it will try and connect to that Wi-Fi network and switch the request over to it once it is connected. The cell network may stay connected if there are outstanding requests (from the same app or from other apps on the system) that match the cell network but not the Wi-Fi network, such as a request with NetworkCapabilities#NET_CAPABILITY_MMS. When a network is no longer needed to serve any request, the system can tear it down at any time and usually does so immediately, so make sure to keep up requests for the networks your app needs.

By contrast, requests filed with ConnectivityManager#registerNetworkCallback will receive callbacks for all matching networks, and will not cause the system to keep up the networks they match. Use this to listen to networks that the device is connected to, but that you don't want the system to keep up for your use case.

Applications build a NetworkRequest and pass it to one of the ConnectivityManager methods above together with a ConnectivityManager.NetworkCallback object. The callback will then start receiving method calls about networks that match the request.

Networks are brought up and/or matched according to the capabilities set in the builder. For example, a request with NetworkCapabilities#NET_CAPABILITY_MMS lets the system match and/or bring up a network that is capable to send MMS. A request with NetworkCapabilities#NET_CAPABILITY_NOT_METERED matches a network that doesn't charge the user for usage. See NetworkCapabilities for a list of capabilities and their description.

While all capabilities can be matched with the ConnectivityManager#registerNetworkCallback family of methods, not all capabilities can be used to request that the system brings up a network with ConnectivityManager#requestNetwork. For example, an application cannot use ConnectivityManager#requestNetwork to ask the system to bring up a network with NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL, because the system won't know if a network has a captive portal before it connects to that network. Similarly, some capabilities may require a specific permission or privilege to be requested. Look up the specific capability and the ConnectivityManager#requestNetwork method for limitations applicable to each capability.

Also, starting with Build.VERSION_CODES#UPSIDE_DOWN_CAKE, some capabilities require the application to self-certify by explicitly adding the PackageManager.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES property in the AndroidManifest.xml, which points to an XML resource file. In the XML resource file, the application declares what kind of network capabilities the application wants to have. Here is an example self-certification XML resource file :

  <network-capabilities-declaration xmlns:android="http://schemas.android.com/apk/res/android">
     <uses-network-capability android:name="NET_CAPABILITY_PRIORITIZE_LATENCY"/>
     <uses-network-capability android:name="NET_CAPABILITY_PRIORITIZE_BANDWIDTH"/>
 </network-capabilities-declaration>
  
  
Look up the specific capability to learn whether its usage requires this self-certification.

Summary

Nested classes

class NetworkRequest.Builder

Builder used to create NetworkRequest objects. 

Inherited constants

Fields

public static final Creator<NetworkRequest> CREATOR

Public methods

boolean canBeSatisfiedBy(NetworkCapabilities nc)

Returns true if and only if the capabilities requested in this NetworkRequest are satisfied by the provided NetworkCapabilities.

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

int[] getCapabilities()

Gets all the capabilities set on this NetworkRequest instance.

NetworkSpecifier getNetworkSpecifier()
int[] getTransportTypes()

Gets all the transports set on this NetworkRequest instance.

boolean hasCapability(int capability)
boolean hasTransport(int transportType)
int hashCode()

Returns a hash code value for the object.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Inherited methods

Fields

CREATOR

Added in API level 21
Also in U Extensions 1
public static final Creator<NetworkRequest> CREATOR

Public methods

canBeSatisfiedBy

Added in API level 30
Also in U Extensions 1
public boolean canBeSatisfiedBy (NetworkCapabilities nc)

Returns true if and only if the capabilities requested in this NetworkRequest are satisfied by the provided NetworkCapabilities.

Parameters
nc NetworkCapabilities: Capabilities that should satisfy this NetworkRequest. null capabilities do not satisfy any request.

Returns
boolean

describeContents

Added in API level 21
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

equals

Added in API level 21
public boolean equals (Object obj)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj Object: This value may be null.

Returns
boolean true if this object is the same as the obj argument; false otherwise.

getCapabilities

Added in API level 31
Also in U Extensions 1
public int[] getCapabilities ()

Gets all the capabilities set on this NetworkRequest instance.

Returns
int[] an array of capability values for this instance. This value cannot be null. 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, or android.net.NetworkCapabilities.NET_CAPABILITY_LOCAL_NETWORK

getNetworkSpecifier

Added in API level 30
Also in U Extensions 1
public NetworkSpecifier getNetworkSpecifier ()

Returns
NetworkSpecifier This value may be null.

getTransportTypes

Added in API level 31
Also in U Extensions 1
public int[] getTransportTypes ()

Gets all the transports set on this NetworkRequest instance.

Returns
int[] an array of transport type values for this instance. This value cannot be null. 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, or NetworkCapabilities.TRANSPORT_THREAD

hasCapability

Added in API level 28
Also in U Extensions 1
public boolean hasCapability (int capability)

Parameters
capability int: 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, or android.net.NetworkCapabilities.NET_CAPABILITY_LOCAL_NETWORK

Returns
boolean

hashCode

Added in API level 21
public int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Returns
int a hash code value for this object.

toString

Added in API level 21
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

writeToParcel

Added in API level 21
public void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written. This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES