测试网络的物理连接
只是物理连接上,并非实际网络连接(可上网)
关于判断代码中,有 available 和 connect 的区别。
* isAvailable的解释是:Indicates whether network connectivity is possible. 表明网络连接是否posible,posible可能的意思,而并不是已经连接
* isConnected():Indicates whether network connectivity exists and it is possible to establish connections and pass data.表明网络连接是否存在并且可以传递数据。
在WLAN设置界面
1,显示连接已保存,但标题栏没有,即没有实质连接上 输出为:not connect, available
2,显示连接已保存,标题栏也有已连接上的图标, 输出为:connect, available
3,选择不保存后 输出为:not connect, available
4,选择连接,在正在获取IP地址时 输出为:not connect, not available
5,连接上后 输出为:connect, available
available:
判断是否有可用状态的Wifi,以下情况返回false:
1. 设备wifi开关关掉;
2. 已经打开飞行模式;
3. 设备所在区域没有信号覆盖;
4. 设备在漫游区域,且关闭了网络漫游。
1)判断是否有网络连接
public boolean isNetworkConnected(Context context) {
if (context == null) {
return false;
}
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connectivity.getAllNetworks();
if (networks != null && networks.length > 0){
NetworkInfo networkInfo;
for (Network mNetwork : networks) {
networkInfo = connectivity.getNetworkInfo(mNetwork);
if (networkInfo != null
&& networkInfo.getState()!= null
&& networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
return true;
}
}
}
} else {
NetworkInfo[] infos = connectivity.getAllNetworkInfo();
if (infos != null && infos.length >0) {
for (NetworkInfo ni : infos) {
if (ni != null && ni.isConnected()) {
return true;
}
}
}
}
}
return false;
}
2)判断WIFI网络是否可用
public boolean isWifiConnected(Context context) {
if (context != null) {
try {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnectivityManager != null){
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isConnected();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
return false;
}
3)判断MOBILE网络是否可用
public boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnectivityManager != null){
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isConnected();
}
}
}
return false;
}
4)判断有线网络是否可用
public boolean isEthernetConnected(Context context) {
if (context != null){
try {
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connManager != null) {
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (networkInfo != null) {
return networkInfo.isConnected();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
return false;
}
5)获取当前网络连接的类型信息
// ConnectivityManager.TYPE_WIFI:
// ConnectivityManager.TYPE_MOBILE
public int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
判断网络的真连接
通过ping的方法
// 是否正在ping
private boolean mIsPinging = false;
// 上一次ping的时间
private long mLastPingTime;
// 两次ping 的间隔(30min)
private final long PING_INTERVAL = 30 * 60* 1000L;
public boolean canDoPinging(){
if (mIsPinging){
AppLog.d("正在执行ping,just return");
return false;
}else if (System.currentTimeMillis() - mLastPingTime < PING_INTERVAL){
AppLog.d("两次ping间隔小于"+PING_INTERVAL+" ,skip");
return false;
}
return true;
}
/* @author suncat
* @category 判断是否有外网连接(普通方法不能判断外网的网络是否连接,比如连接上局域网)
* @return
*/
public boolean ping() {
if (!canDoPinging()){
return false;
}
mIsPinging = true;
mLastPingTime = System.currentTimeMillis();
String result = null;
try {
String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网
Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次
// 读取ping的内容,可以不加
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
Log.d("------ping-----", "result content : " + stringBuffer.toString());
// ping的状态
int status = p.waitFor();
/**
* 连通的移动数据网络下:0
* 需要网页认证的 Wi-Fi 下:1
* Wi-Fi 打开但没有网络连接,数据也不可用的状态下:2
* 在不可用的 Wi-Fi 下:2
* 在可用的 Wi-Fi 下:0
*
* 致命的缺点,当网络未连通时,它可以马上返回 false,但是当网络连通时,它需要耗费至少 10 秒的时间才能返回结果(单次)。
* 100 packets transmitted, 0 received, 100% packet loss, time 99023ms
* */
if (status == 0) {
result = "success";
mIsPinging = false;
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("----result---", "result = " + result);
}
mIsPinging = false;
return false;
}
通过NetworkCapabilities(need Android 6.0)不可靠方法
经过测试,创维盒子(android 7.0)上 无论怎么样返回 true
该方法是理解返回的,无需和ping一样等待
public boolean isNetworkRealConnect(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnectivityManager != null){
NetworkCapabilities networkCapabilities = null;
networkCapabilities = mConnectivityManager.getNetworkCapabilities(mConnectivityManager.getActiveNetwork());
// Indicates that connectivity on this network was successfully validated.
// For example, for a network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully detected.
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}else {
throw new RuntimeException("need android 6.0 or up");
}
return false;
}