android_判断网络链接状态-ConnectivityManager

eg:
if (NetHelper.isNetworkAvailable(getApplication())) {
Toast.makeText(getApplication(), “网络链接成功”,
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplication(), “网络连接异常,请稍后再试!”,
Toast.LENGTH_LONG).show();
}

public class NetHelper {

public static boolean isNetworkAvailable(Context context) {
    //判断网络是否连通 ConnectivityManager connectivity=context.getSystemService(Context.CONNECTIVITY_SERVICE);
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {//connectivity为空 网络不通
        return false;
    } else {
        //getAllNetworkInfo 获取设备支持的所有网络类型的链接状态信息
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                //CONNECTED 已连接
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

}
ConnectivityManager相关api
NetworkInfo getActiveNetworkInfo() 获取当前连接可用的网络
NetworkInfo[] getAllNetworkInfo() 获取设备支持的所有网络类型的链接状态信息。
NetworkInfo getNetworkInfo(int networkType) 获取特定网络类型的链接状态信息
int getNetworkPreference() 获取当前偏好的网络类型。
boolean isActiveNetworkMetered() Returns if the currently active data network is metered.
static boolean isNetworkTypeValid(int networkType) 判断给定的数值是否表示一种网络
boolean requestRouteToHost(int networkType, int hostAddress)
Ensure that a network route exists to deliver traffic to the specified host via the specified network interface.
void setNetworkPreference(int preference)
Specifies the preferred network type.
int startUsingNetworkFeature(int networkType, String feature)
Tells the underlying networking system that the caller wants to begin using the named feature.
int stopUsingNetworkFeature(int networkType, String feature)
Tells the underlying networking system that the caller is finished using the named feature.

NetworkInfo详解

    NetworkInfo是一个描述网络状态的接口,可通过ConnectivityManager调用getActiveNetworkInfo()获得当前连接的网络类型。

    NetworkInfo有两个枚举类型的成员变量NetworkInfo.DetailedState和NetworkInfo.State,用于查看当前网络的状态。其中NetworkInfo.State的值包括:

NetworkInfo.State CONNECTED
已连接
NetworkInfo.State CONNECTING
正在连接
NetworkInfo.State DISCONNECTED

NetworkInfo.State DISCONNECTING

NetworkInfo.State SUSPENDED

NetworkInfo.State UNKNOWN

    NetworkInfo.DetailedState则状态描述更为详细。


    NetworkInfo还包括一系列可用的方法用于判断当前网络是否可用,如下:

Public Methods
NetworkInfo.DetailedState getDetailedState()
Reports the current fine-grained state of the network.
String getExtraInfo()
Report the extra information about the network state, if any was provided by the lower networking layers., if one is available.
String getReason()
如果数据网络连接可用,但是连接失败,则通过此方法可获得尝试链接失败的原因
Report the reason an attempt to establish connectivity failed, if one is available.
NetworkInfo.State getState()
获取网络连接的粗粒度状态
Reports the current coarse-grained state of the network.
int getSubtype()
Return a network-type-specific integer describing the subtype of the network.
String getSubtypeName()
Return a human-readable name describing the subtype of the network.
int* getType()*
报告当前网络从属的网络类型
Reports the type of network to which the info in this NetworkInfo pertains.
String
getTypeName()报告当前网络从属的网络类型,更明确的方式如wifi,和mobile等。
Return a human-readable name describe the type of the network, for example “WIFI” or “MOBILE”.
boolean
isAvailable() 判断当前网络是否可用
Indicates whether network connectivity is possible.
boolean
isConnected() 判断当前网络是否存在,并可用于数据传输
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
boolean
isConnectedOrConnecting()
Indicates whether network connectivity exists or is in the process of being established.
boolean
isFailover()
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.
boolean
isRoaming() 判断设备当前是否在网络上漫游
Indicates whether the device is currently roaming on this network.
String
toString() 返回一个包含该网络的简单的易懂的字符串描述。
Returns a string containing a concise, human-readable description of this object.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值