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")) { Log.d(Constants.TAG, "get Wifi connection"); if (net.isConnected()) { hasWifoCon = true; } } if (type.equalsIgnoreCase("MOBILE")) { Log.d(Constants.TAG, "get Mobile connection"); if (net.isConnected()) { hasMobileCon = true; } } } return hasWifoCon || hasMobileCon; }
检查WiFi或移动网络是否可用
最新推荐文章于 2021-05-29 20:37:11 发布