android_判断网络链接状态-ConnectivityManager
来源:互联网 发布:网络性能监控软件 编辑:程序博客网 时间:2024/06/05 00:57
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.
- android_判断网络链接状态-ConnectivityManager
- Android_判断手机网络状态
- ConnectivityManager网络连接状态
- Android判断网络是否断开(WIFI 状态)--ConnectivityManager
- ConnectivityManager判断网络状态及wifi,数据流量
- ConnectivityManager简介,网络连接状态
- Android网络状态之----ConnectivityManager
- Android网络状态之ConnectivityManager
- ConnectivityManager判断是否有网络
- 用ConnectivityManager判断网络类型
- ConnectivityManager判断是否有网络
- ConnectivityManager 判断网络是否可用
- Android判断网络链接状态
- ConnectivityManager类网络状态判断设置+BaseActivity和BaseFragment的封装使用+网络状态工具类+网络请求工具类
- android学习——ConnectivityManager 简单的使用 判断网络状态
- android之ConnectivityManager简介,网络连接状态
- android之ConnectivityManager简介,网络连接状态
- android之ConnectivityManager简介,网络连接状态
- 深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量
- openssl 编译
- java FTP 工具类 同步文件夹 下载文件..等操作(FtpHelper 需要commons-net-3.5.jar)
- Android 简单的验证码效果
- SQL Server 中可用的索引类型-温故而知新
- android_判断网络链接状态-ConnectivityManager
- 解决羊生羊问题
- IIS中HTTP Error 503. The service is unavailable
- matlab-自控原理 两个传递函数并联
- 《离散数学及其应用》读书笔记【二】集合 函数 数列 求和
- http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22891521&id=2109284
- 影响到Spark输出RDD分区的操作函数
- POJ 2386
- (5)LR实战之Discuz开源论坛——网页细分图结果分析(Web Page Diagnostics)