android 网络连接 网络是否可用,Android 使用ping判断网络/WIFI连接是否可用
最近項目中有一個網絡判斷的需求,因為終端只能連接wifi,wifi即使連接上也可能會出現不能連接網絡的問題,所以需要進行網絡連接的判斷。
ping的相關知識
使用ping檢驗網絡是否可以連接
ping就是檢測客戶端是否可以上網。
那么我們就上代碼
該段代碼來自http://blankj.com
導入常用工具包compile 'com.blankj:utilcode:1.9.8'
這個里邊有各種各樣的常用的工具類,下邊的代碼是NetworkUtils的一個方法;
/**
* 判斷網絡是否可用
*
需添加權限 {@code }
*
需要異步ping,如果ping不通就說明網絡不可用
*
* @param ip ip地址(自己服務器ip),如果為空,ip為阿里巴巴公共ip
* @return {@code true}: 可用
{@code false}: 不可用
*/
public static boolean isAvailableByPing(String ip) {
if (ip == null || ip.length() <= 0) {
ip = "223.5.5.5";// 阿里巴巴公共ip
}
ShellUtils.CommandResult result = ShellUtils.execCmd(String.format("ping -c 1 %s", ip), false);
boolean ret = result.result == 0;
if (result.errorMsg != null) {
Log.d("NetworkUtils", "isAvailableByPing() called" + result.errorMsg);
}
if (result.successMsg != null) {
Log.d("NetworkUtils", "isAvailableByPing() called" + result.successMsg);
}
return ret;
}
這段代碼完全可以作為網絡檢測的使用,可是我在使用這段代碼的時候偶爾會遇到anr的問題。出現anr就說明在主線程做了耗時操作,這是我沒有將這段代碼放到子線程造成的問題。其實ping也就相當于去請求網絡,跟普通的接口請求差不多,當網絡比較慢的時候就會消耗時間,放在主線程就會造成anr。
在修改bug的時候我對這段代碼進行了一些修改:
* 判斷網絡是否可用
*
需添加權限 {@code }
*
需要異步ping,如果ping不通就說明網絡不可用
*
* @param ip ip地址(自己服務器ip),如果為空,ip為阿里巴巴公共ip
* @return {@code true}: 可用
{@code false}: 不可用
*/
public static boolean isAvailableByPing(String ip) {
if (ip == null || ip.length() <= 0) {
ip = "223.5.5.5";// 阿里巴巴公共ip
}
Runtime runtime = Runtime.getRuntime();
Process ipProcess = null;
try {
//-c 后邊跟隨的是重復的次數,-w后邊跟隨的是超時的時間,單位是秒,不是毫秒,要不然也不會anr了
ipProcess = runtime.exec("ping -c 3 -w 3 "+ip);
int exitValue = ipProcess.waitFor();
Log.i("Avalible", "Process:" + exitValue);
return (exitValue == 0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
//在結束的時候應該對資源進行回收
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
}
return false;
}
只需要把這個方法引用到子線程中就行了,其實不引用也可以,因為在這里設定了超時時間為3秒。但是最好是放到子線程里邊。
問題補充
上邊的代碼在Android 7.1.2中出現了問題,好好的WIFI連接上之后,ping不通,每次waitfor都返回1,返回一表示WiFi連接,但是沒有網絡。其實是有網絡的,很奇怪。
解決方法:
這里我獲取了ping時抓包的信息,判斷是否丟包為100%。如果為100%,就說明網絡有問題。不是100%說明網絡連接沒問題。
廢話不多說,直接上代碼!老鐵雙擊666,,哈哈😄
public boolean isNetworkOnline() {
Runtime runtime = Runtime.getRuntime();
Process ipProcess = null;
try {
ipProcess = runtime.exec("ping -c 5 -w 4 223.5.5.5");
InputStream input = ipProcess.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
int exitValue = ipProcess.waitFor();
if (exitValue == 0) {
//WiFi連接,網絡正常
return true;
} else {
if (stringBuffer.indexOf("100% packet loss") != -1) {
//網絡丟包嚴重,判斷為網絡未連接
return false;
} else {
//網絡未丟包,判斷為網絡連接
return true;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
if (ipProcess != null) {
ipProcess.destroy();
}
runtime.gc();
}
return false;
}
總結
以上是生活随笔為你收集整理的android 网络连接 网络是否可用,Android 使用ping判断网络/WIFI连接是否可用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 改革以来计算机应用发展总结,计算机应用基
- 下一篇: winform教_电脑绝技教你22天学精