百度搜索 如何判断android网络是否可用,会有一大堆的结果。
但是最终结底还是用networkInfo.isAvailable来判断的,但是经过我的实验,我发现此方法只是判断手机联网状态是否就绪,而不是真的判断忘络是否已经链接。
下面贴出我的测试经过。
测试机型:华为荣耀3C ROM miui v6(其他机型是否如此有待验证,且真机和虚拟的结果肯定是有出入,所以这方面最好用真机来测试)
测试代码:
//AndroidManifest.xml中需加入权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] networkInfos = cm.getAllNetworkInfo(); for (NetworkInfo info : networkInfos) { Log.i("AAA", info.getTypeName() + " is connected " + info.isConnected() + " isAvailable " + info.isAvailable()); } NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null) Log.i("AAA", "network type is " + info.getTypeName() + " network is available " + info.isAvailable() + " network is connected " + info.isConnected());
测试经过:
第一步:WIFI打开并连接网络,数据开关打开。打印结果:
mobile is connected false isAvailable true WIFI is connected true isAvailable true mobile_mms is connected false isAvailable true mobile_supl is connected false isAvailable true mobile_dun is connected false isAvailable true mobile_hipri is connected false isAvailable true BLUETOOTH_TETHER is connected false isAvailable false ETHERNET is connected false isAvailable false mobile_fota is connected false isAvailable true mobile_ims is connected false isAvailable true mobile_cbs is connected false isAvailable true active network type is WIFI network is available true network is connected true
第二步 :WIFI打开,但是没有连接网络,数据开关打开连接数据流量。打印结果:
mobile is connected true isAvailable true WIFI is connected false isAvailable true mobile_mms is connected false isAvailable true mobile_supl is connected false isAvailable true mobile_dun is connected false isAvailable true mobile_hipri is connected false isAvailable true BLUETOOTH_TETHER is connected false isAvailable false ETHERNET is connected false isAvailable false mobile_fota is connected false isAvailable true mobile_ims is connected false isAvailable true mobile_cbs is connected false isAvailable true active network type is mobile network is available true network is connected true
经过上面对比可知:当wifi打开时,数据连接其实是没有连接的。wifi开关只要是打开的,不管有没有连接wifi ,wifi都是Available的。
第三步:关闭wifi开关,数据开关开始并已经连接数据流量。 打印结果:
mobile is connected true isAvailable true WIFI is connected false isAvailable false mobile_mms is connected false isAvailable true mobile_supl is connected false isAvailable true mobile_dun is connected false isAvailable true mobile_hipri is connected false isAvailable true BLUETOOTH_TETHER is connected false isAvailable false ETHERNET is connected false isAvailable false mobile_fota is connected false isAvailable true mobile_ims is connected false isAvailable true mobile_cbs is connected false isAvailable true active network type is mobile network is available true network is connected true
结论1:只有当wifi关闭的时候 wifi的available状态才是false的
第四步:关闭wifi开关和数据开关。打印结果:
mobile is connected false isAvailable true WIFI is connected false isAvailable false mobile_mms is connected false isAvailable true mobile_supl is connected false isAvailable true mobile_dun is connected false isAvailable true mobile_hipri is connected false isAvailable true BLUETOOTH_TETHER is connected false isAvailable false ETHERNET is connected false isAvailable false mobile_fota is connected false isAvailable true mobile_ims is connected false isAvailable true mobile_cbs is connected false isAvailable true
发现,当wifi开关关闭的时候 wifi的available状态是 false的,但是当关闭数据连接的开关的时候,mobile的连接状态 isConnected虽然是false但是Available仍然是true的。
而且最后一行Log没有打出来说明此时getActiveNetworkInfo()的返回值是null
上面的四个步骤都是在手机SIM卡有信号的情况下测试的。也就是说只要打开数据开关数据连接就能连上的情况下测试的。正常情况下应该还要测试当手机卡没有信号的情况下的结果,但是这种情况不太好模拟,所以我就到
sim卡管理中心把sim给禁用掉了。具体没有研究过,所以不太清楚这种情况能不能模拟手机没有信号的情况这个待定。
打印的结果如下:
mobile is connected false isAvailable false WIFI is connected false isAvailable false mobile_mms is connected false isAvailable false mobile_supl is connected false isAvailable false mobile_dun is connected false isAvailable false mobile_hipri is connected false isAvailable false BLUETOOTH_TETHER is connected false isAvailable false ETHERNET is connected false isAvailable false mobile_fota is connected false isAvailable false mobile_ims is connected false isAvailable false mobile_cbs is connected false isAvailable false
不出意外,此时的mobile的available状态已经成为了false。
其实通过NetworkInfo的api中就可以看出来
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.表明网络连接是否存在并且可以传递数据。
这两处的连接用的是 connectivity名词。在此处可以理解为 “建立链接”这一事实。(本人英语渣渣般的存在,如果不对,请无视)。
当然百度搜索网络连接是否可用,给的结果用isAvailable判断是否可用也无可厚非,因为这个确实是判断网络是否可用的,谷歌给出的解释也是这个意思,只是这个地方的可用只是一种可能性的判断,是判断网络状态是否已经准备好可以用来建立连接了。
所以,判断网络是否已经建立链接,并且可以上网了的判断应该是酱紫的:
public boolean isNetWorkConnected() { boolean isConnected = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeInfo = cm.getActiveNetworkInfo(); if (activeInfo != null && activeInfo.isAvailable() && activeInfo.isConnected()) { isConnected = true; } return isConnected; }
应该就是这样。
至于像腾讯的那种wifi链接上,但是连接像CMCC这种没有登录账号就不能用的wifi后提示网络不可用的方法。暂时没有从api中找到思路,不过看到有网友说可以请求一下一个接口,如果TimeOut的话就是wifi不可用,私以为此方法可行,如果你感觉你们的服务器稳定性不是很好,可以背靠大公司呀,向BAT这样的网站发起一个http请求,如果响应超时,我想一般情况下就是你手机网络设置的问题了。(当然我大企鹅的肯定没有这么简单,他们是有socket连接的,心里默念企鹅大法好,BAT技术的牛逼性大家有目共睹。)
以上。
如果有不对的地方还请指正!