
正文
Android判断网络是否已经连接
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
// check all network connect, WIFI or mobile
public static boolean isNetworkAvailable(final Context context) {
boolean hasWifoCon = false;
boolean hasMobileCon = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfos = cm.getAllNetworkInfo();
for (NetworkInfo net : netInfos) { String type = net.getTypeName();
if (type.equalsIgnoreCase("WIFI")) {
LevelLogUtils.getInstance().i(tag, "get Wifi connection");
if (net.isConnected()) {
hasWifoCon = true;
}
} if (type.equalsIgnoreCase("MOBILE")) {
LevelLogUtils.getInstance().i(tag, "get Mobile connection");
if (net.isConnected()) {
hasMobileCon = true;
}
}
}
return hasWifoCon || hasMobileCon; }








