FileDocCategorySizeDatePackage
NetworkInfo.javaAPI DocAndroid 1.5 API13361Wed May 06 22:41:54 BST 2009android.net

NetworkInfo

public class NetworkInfo extends Object implements android.os.Parcelable
Describes the status of a network interface of a given type (currently either Mobile or Wifi).

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
mIsAvailable
Indicates whether network connectivity is possible:
public static final Creator
CREATOR
Implement the Parcelable interface.
Constructors Summary
public NetworkInfo(int type)

param
type network type
deprecated
hide
because this constructor was only meant for internal use (and has now been superseded by the package-private constructor below).

NetworkInfo(int type, int subtype, String typeName, String subtypeName)

        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 = true;
        mIsRoaming = false;
    
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

        return mDetailedState;
    
public java.lang.StringgetExtraInfo()
Report the extra information about the network state, if any was provided by the lower networking layers., if one is available.

return
the extra information, or null if not available

        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

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

return
the coarse-grained state

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

return
the network subtype

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

return
the name of the network subtype

        return mSubtypeName;
    
public intgetType()
Reports the type of network (currently mobile or Wi-Fi) to which the info in this object pertains.

return
the network type

        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

        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

        return mIsAvailable;
    
public booleanisConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.

return
{@code true} if network connectivity exists, {@code false} otherwise.

        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.

        return mState == State.CONNECTED || mState == State.CONNECTING;
    
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.

        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.

        return mIsRoaming;
    
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.

        this.mDetailedState = detailedState;
        this.mState = stateMap.get(detailedState);
        this.mReason = reason;
        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

        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

        mIsAvailable = isAvailable;
    
voidsetRoaming(boolean isRoaming)

        mIsRoaming = isRoaming;
    
voidsetSubtype(int subtype, java.lang.String subtypeName)

        mSubtype = subtype;
        mSubtypeName = subtypeName;
    
public java.lang.StringtoString()

        StringBuilder builder = new StringBuilder("NetworkInfo: ");
        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);
        return builder.toString();
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface.

hide

        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.writeString(mReason);
        dest.writeString(mExtraInfo);