FileDocCategorySizeDatePackage
NetworkInfo.javaAPI DocAndroid 5.1 API17169Thu Mar 12 22:22:10 GMT 2015android.net

NetworkInfo

public class NetworkInfo extends Object implements android.os.Parcelable
Describes the status of a network interface.

Use {@link ConnectivityManager#getActiveNetworkInfo()} to get an instance that represents the current network connection.

Fields Summary
private static final EnumMap
stateMap
This is the map described in the Javadoc comment above. The positions of the elements of the array must correspond to the ordinal values of DetailedState.
private int
mNetworkType
private int
mSubtype
private String
mTypeName
private String
mSubtypeName
private State
mState
private DetailedState
mDetailedState
private String
mReason
private String
mExtraInfo
private boolean
mIsFailover
private boolean
mIsRoaming
private boolean
mIsConnectedToProvisioningNetwork
private boolean
mIsAvailable
Indicates whether network connectivity is possible:
public static final Creator
CREATOR
Implement the Parcelable interface.
Constructors Summary
public NetworkInfo(int type, int subtype, String typeName, String subtypeName)

hide

        if (!ConnectivityManager.isNetworkTypeValid(type)) {
            throw new IllegalArgumentException("Invalid network type: " + type);
        }
        mNetworkType = type;
        mSubtype = subtype;
        mTypeName = typeName;
        mSubtypeName = subtypeName;
        setDetailedState(DetailedState.IDLE, null, null);
        mState = State.UNKNOWN;
        mIsAvailable = false; // until we're told otherwise, assume unavailable
        mIsRoaming = false;
        mIsConnectedToProvisioningNetwork = false;
    
public NetworkInfo(NetworkInfo source)
{@hide}

        if (source != null) {
            synchronized (source) {
                mNetworkType = source.mNetworkType;
                mSubtype = source.mSubtype;
                mTypeName = source.mTypeName;
                mSubtypeName = source.mSubtypeName;
                mState = source.mState;
                mDetailedState = source.mDetailedState;
                mReason = source.mReason;
                mExtraInfo = source.mExtraInfo;
                mIsFailover = source.mIsFailover;
                mIsRoaming = source.mIsRoaming;
                mIsAvailable = source.mIsAvailable;
                mIsConnectedToProvisioningNetwork = source.mIsConnectedToProvisioningNetwork;
            }
        }
    
Methods Summary
public intdescribeContents()
Implement the Parcelable interface

hide

        return 0;
    
public android.net.NetworkInfo$DetailedStategetDetailedState()
Reports the current fine-grained state of the network.

return
the fine-grained state

        synchronized (this) {
            return mDetailedState;
        }
    
public java.lang.StringgetExtraInfo()
Report the extra information about the network state, if any was provided by the lower networking layers.

return
the extra information, or null if not available

        synchronized (this) {
            return mExtraInfo;
        }
    
public java.lang.StringgetReason()
Report the reason an attempt to establish connectivity failed, if one is available.

return
the reason for failure, or null if not available

        synchronized (this) {
            return mReason;
        }
    
public android.net.NetworkInfo$StategetState()
Reports the current coarse-grained state of the network.

return
the coarse-grained state

        synchronized (this) {
            return mState;
        }
    
public intgetSubtype()
Return a network-type-specific integer describing the subtype of the network.

return
the network subtype

        synchronized (this) {
            return mSubtype;
        }
    
public java.lang.StringgetSubtypeName()
Return a human-readable name describing the subtype of the network.

return
the name of the network subtype

        synchronized (this) {
            return mSubtypeName;
        }
    
public intgetType()
Reports the type of network to which the info in this {@code NetworkInfo} pertains.

return
one of {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_WIMAX}, {@link ConnectivityManager#TYPE_ETHERNET}, {@link ConnectivityManager#TYPE_BLUETOOTH}, or other types defined by {@link ConnectivityManager}

        synchronized (this) {
            return mNetworkType;
        }
    
public java.lang.StringgetTypeName()
Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE".

return
the name of the network type

        synchronized (this) {
            return mTypeName;
        }
    
public booleanisAvailable()
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.

return
{@code true} if the network is available, {@code false} otherwise

        synchronized (this) {
            return mIsAvailable;
        }
    
public booleanisConnected()
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
{@code true} if network connectivity exists, {@code false} otherwise.

        synchronized (this) {
            return mState == State.CONNECTED;
        }
    
public booleanisConnectedOrConnecting()
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 {@link #isConnected()} instead, which guarantees that the network is fully usable.

return
{@code true} if network connectivity exists or is in the process of being established, {@code false} otherwise.

        synchronized (this) {
            return mState == State.CONNECTED || mState == State.CONNECTING;
        }
    
public booleanisConnectedToProvisioningNetwork()
{@hide}

        synchronized (this) {
            return mIsConnectedToProvisioningNetwork;
        }
    
public booleanisFailover()
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
{@code true} if this is a failover attempt, {@code false} otherwise.

        synchronized (this) {
            return mIsFailover;
        }
    
public booleanisRoaming()
Indicates whether the device is currently roaming on this network. When {@code true}, it suggests that use of data on this network may incur extra costs.

return
{@code true} if roaming is in effect, {@code false} otherwise.

        synchronized (this) {
            return mIsRoaming;
        }
    
public voidsetDetailedState(android.net.NetworkInfo$DetailedState detailedState, java.lang.String reason, java.lang.String extraInfo)
Sets the fine-grained state of the network.

param
detailedState the {@link DetailedState}.
param
reason a {@code String} indicating the reason for the state change, if one was supplied. May be {@code null}.
param
extraInfo an optional {@code String} providing addditional network state information passed up from the lower networking layers.
hide

        synchronized (this) {
            this.mDetailedState = detailedState;
            this.mState = stateMap.get(detailedState);
            this.mReason = reason;
            this.mExtraInfo = extraInfo;
        }
    
public voidsetExtraInfo(java.lang.String extraInfo)
Set the extraInfo field.

param
extraInfo an optional {@code String} providing addditional network state information passed up from the lower networking layers.
hide

        synchronized (this) {
            this.mExtraInfo = extraInfo;
        }
    
public voidsetFailover(boolean isFailover)
Set the failover boolean.

param
isFailover {@code true} to mark the current connection attempt as a failover.
hide

        synchronized (this) {
            mIsFailover = isFailover;
        }
    
public voidsetIsAvailable(boolean isAvailable)
Sets if the network is available, ie, if the connectivity is possible.

param
isAvailable the new availability value.
hide

        synchronized (this) {
            mIsAvailable = isAvailable;
        }
    
public voidsetIsConnectedToProvisioningNetwork(boolean val)
{@hide}

        synchronized (this) {
            mIsConnectedToProvisioningNetwork = val;
        }
    
public voidsetRoaming(boolean isRoaming)
{@hide}

        synchronized (this) {
            mIsRoaming = isRoaming;
        }
    
public voidsetSubtype(int subtype, java.lang.String subtypeName)

hide

        synchronized (this) {
            mSubtype = subtype;
            mSubtypeName = subtypeName;
        }
    
public voidsetType(int type)

hide

        synchronized (this) {
            mNetworkType = type;
        }
    
public java.lang.StringtoString()

        synchronized (this) {
            StringBuilder builder = new StringBuilder("[");
            builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()).
            append("], state: ").append(mState).append("/").append(mDetailedState).
            append(", reason: ").append(mReason == null ? "(unspecified)" : mReason).
            append(", extra: ").append(mExtraInfo == null ? "(none)" : mExtraInfo).
            append(", roaming: ").append(mIsRoaming).
            append(", failover: ").append(mIsFailover).
            append(", isAvailable: ").append(mIsAvailable).
            append(", isConnectedToProvisioningNetwork: ").
            append(mIsConnectedToProvisioningNetwork).
            append("]");
            return builder.toString();
        }
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface.

hide

        synchronized (this) {
            dest.writeInt(mNetworkType);
            dest.writeInt(mSubtype);
            dest.writeString(mTypeName);
            dest.writeString(mSubtypeName);
            dest.writeString(mState.name());
            dest.writeString(mDetailedState.name());
            dest.writeInt(mIsFailover ? 1 : 0);
            dest.writeInt(mIsAvailable ? 1 : 0);
            dest.writeInt(mIsRoaming ? 1 : 0);
            dest.writeInt(mIsConnectedToProvisioningNetwork ? 1 : 0);
            dest.writeString(mReason);
            dest.writeString(mExtraInfo);
        }