Added in API level 1
Deprecated in API level 29

NetworkInfo


open class NetworkInfo : Parcelable
kotlin.Any
   ↳ android.net.NetworkInfo

Describes the status of a network interface.

Use ConnectivityManager.getActiveNetworkInfo() to get an instance that represents the current network connection.

Summary

Nested classes

The fine-grained state of a network connection.

Coarse-grained network state.

Inherited constants
Public constructors
NetworkInfo(type: Int, subtype: Int, typeName: String?, subtypeName: String?)

Create a new instance of NetworkInfo.

Public methods
open Int

open NetworkInfo.DetailedState

Reports the current fine-grained state of the network.

open String!

Report the extra information about the network state, if any was provided by the lower networking layers.

open String!

Report the reason an attempt to establish connectivity failed, if one is available.

open NetworkInfo.State!

Reports the current coarse-grained state of the network.

open Int

Return a network-type-specific integer describing the subtype of the network.

open String!

Return a human-readable name describing the subtype of the network.

open Int

Reports the type of network to which the info in this NetworkInfo pertains.

open String!

Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE".

open Boolean

Indicates whether network connectivity is possible.

open Boolean

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

open Boolean

Indicates whether network connectivity exists or is in the process of being established.

open Boolean

Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network.

open Boolean

Indicates whether the device is currently roaming on this network.

open Unit
setDetailedState(detailedState: NetworkInfo.DetailedState, reason: String?, extraInfo: String?)

Sets the fine-grained state of the network.

open String

open Unit
writeToParcel(dest: Parcel, flags: Int)

Properties
static Parcelable.Creator<NetworkInfo!>

Public constructors

NetworkInfo

Added in API level 30
NetworkInfo(
    type: Int,
    subtype: Int,
    typeName: String?,
    subtypeName: String?)

Create a new instance of NetworkInfo. This may be useful for apps to write unit tests.

Parameters
type Int: the legacy type of the network, as one of the ConnectivityManager.TYPE_* constants.
subtype Int: the subtype if applicable, as one of the TelephonyManager.NETWORK_TYPE_* constants.
typeName String?: a human-readable string for the network type, or an empty string or null.
subtypeName String?: a human-readable string for the subtype, or an empty string or null.

Public methods

describeContents

Added in API level 1
open fun describeContents(): Int

Deprecated: Deprecated in Java.

Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

getDetailedState

Added in API level 1
open fun getDetailedState(): NetworkInfo.DetailedState

Deprecated: Apps should instead use the android.net.ConnectivityManager.NetworkCallback API to learn about connectivity changes. See android.net.ConnectivityManager#registerDefaultNetworkCallback and android.net.ConnectivityManager#registerNetworkCallback. These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes.

Reports the current fine-grained state of the network.

Return
NetworkInfo.DetailedState the fine-grained state This value cannot be null.

getExtraInfo

Added in API level 1
open fun getExtraInfo(): String!

Deprecated: Use other services e.g. WifiManager to get additional information passed up from the lower networking layers.

Report the extra information about the network state, if any was provided by the lower networking layers.

Return
String! the extra information, or null if not available

getReason

Added in API level 1
Deprecated in API level 28
open fun getReason(): String!

Deprecated: This method does not have a consistent contract that could make it useful to callers.

Report the reason an attempt to establish connectivity failed, if one is available.

Return
String! the reason for failure, or null if not available

getState

Added in API level 1
Deprecated in API level 28
open fun getState(): NetworkInfo.State!

Deprecated: Apps should instead use the android.net.ConnectivityManager.NetworkCallback API to learn about connectivity changes. android.net.ConnectivityManager#registerDefaultNetworkCallback and android.net.ConnectivityManager#registerNetworkCallback. These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes.

Reports the current coarse-grained state of the network.

Return
NetworkInfo.State! the coarse-grained state

getSubtype

Added in API level 3
Deprecated in API level 29
open fun getSubtype(): Int

Deprecated: Use android.telephony.TelephonyManager#getDataNetworkType instead.

Return a network-type-specific integer describing the subtype of the network.

Return
Int the network subtype

getSubtypeName

Added in API level 3
Deprecated in API level 29
open fun getSubtypeName(): String!

Deprecated: Use android.telephony.TelephonyManager#getDataNetworkType instead.

Return a human-readable name describing the subtype of the network.

Return
String! the name of the network subtype

getType

Added in API level 1
Deprecated in API level 28
open fun getType(): Int

Deprecated: Callers should switch to checking NetworkCapabilities.hasTransport instead with one of the NetworkCapabilities#TRANSPORT_* constants : getType and getTypeName cannot account for networks using multiple transports. Note that generally apps should not care about transport; NetworkCapabilities.NET_CAPABILITY_NOT_METERED and NetworkCapabilities.getLinkDownstreamBandwidthKbps are calls that apps concerned with meteredness or bandwidth should be looking at, as they offer this information with much better accuracy.

Reports the type of network to which the info in this NetworkInfo pertains.

Return
Int one of ConnectivityManager.TYPE_MOBILE, android.net.ConnectivityManager#TYPE_WIFI, ConnectivityManager.TYPE_WIMAX, android.net.ConnectivityManager#TYPE_ETHERNET, ConnectivityManager.TYPE_BLUETOOTH, or other types defined by ConnectivityManager.

getTypeName

Added in API level 1
Deprecated in API level 28
open fun getTypeName(): String!

Deprecated: Callers should switch to checking NetworkCapabilities.hasTransport instead with one of the NetworkCapabilities#TRANSPORT_* constants : getType and getTypeName cannot account for networks using multiple transports. Note that generally apps should not care about transport; NetworkCapabilities.NET_CAPABILITY_NOT_METERED and NetworkCapabilities.getLinkDownstreamBandwidthKbps are calls that apps concerned with meteredness or bandwidth should be looking at, as they offer this information with much better accuracy.

Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE".

Return
String! the name of the network type

isAvailable

Added in API level 1
Deprecated in API level 28
open fun isAvailable(): Boolean

Deprecated: Apps should instead use the android.net.ConnectivityManager.NetworkCallback API to learn about connectivity changes. android.net.ConnectivityManager#registerDefaultNetworkCallback and android.net.ConnectivityManager#registerNetworkCallback. These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes.

Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include

  • The device is out of the coverage area for any network of this type.
  • The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
  • The device's radio is turned off, e.g., because airplane mode is enabled.
Since Android L, this always returns true, because the system only returns info for available networks.

Return
Boolean true if the network is available, false otherwise

isConnected

Added in API level 1
open fun isConnected(): Boolean

Deprecated: Apps should instead use the android.net.ConnectivityManager.NetworkCallback API to learn about connectivity changes. See android.net.ConnectivityManager#registerDefaultNetworkCallback and android.net.ConnectivityManager#registerNetworkCallback. These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes.

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Always call this before attempting to perform data transactions.

Return
Boolean true if network connectivity exists, false otherwise.

isConnectedOrConnecting

Added in API level 1
Deprecated in API level 28
open fun isConnectedOrConnecting(): Boolean

Deprecated: Apps should instead use the android.net.ConnectivityManager.NetworkCallback API to learn about connectivity changes. android.net.ConnectivityManager#registerDefaultNetworkCallback and android.net.ConnectivityManager#registerNetworkCallback. These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes.

Indicates whether network connectivity exists or is in the process of being established. This is good for applications that need to do anything related to the network other than read or write data. For the latter, call isConnected() instead, which guarantees that the network is fully usable.

Return
Boolean true if network connectivity exists or is in the process of being established, false otherwise.

isFailover

Added in API level 1
Deprecated in API level 28
open fun isFailover(): Boolean

Deprecated: This field is not populated in recent Android releases, and does not make a lot of sense in a multi-network world.

Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network.

Return
Boolean true if this is a failover attempt, false otherwise.

isRoaming

Added in API level 3
Deprecated in API level 28
open fun isRoaming(): Boolean

Deprecated: Callers should switch to checking NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING instead, since that handles more complex situations, such as VPNs.

Indicates whether the device is currently roaming on this network. When true, it suggests that use of data on this network may incur extra costs.

Return
Boolean true if roaming is in effect, false otherwise.

setDetailedState

Added in API level 30
Deprecated in API level 29
open fun setDetailedState(
    detailedState: NetworkInfo.DetailedState,
    reason: String?,
    extraInfo: String?
): Unit

Deprecated: Use NetworkCapabilities instead.

Sets the fine-grained state of the network. This is only useful for testing.

Parameters
detailedState NetworkInfo.DetailedState: the DetailedState. This value cannot be null.
reason String?: a String indicating the reason for the state change, if one was supplied. May be null.
extraInfo String?: an optional String providing additional network state information passed up from the lower networking layers. This value may be null.

toString

Added in API level 1
open fun toString(): String

Deprecated: Deprecated in Java.

Return
String a string representation of the object.

writeToParcel

Added in API level 1
open fun writeToParcel(
    dest: Parcel,
    flags: Int
): Unit

Deprecated: Deprecated in Java.

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_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES

Properties

CREATOR

Added in API level 24
Deprecated in API level 29
static val CREATOR: Parcelable.Creator<NetworkInfo!>

Deprecated: Deprecated in Java.