(Context c)
| 10 | */ |
| 11 | public class NetworkUtils { |
| 12 | public static boolean isNetworkAvailable(Context c) { |
| 13 | Context context = c.getApplicationContext(); |
| 14 | // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) |
| 15 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 16 | |
| 17 | if (connectivityManager == null) { |
| 18 | return false; |
| 19 | } else { |
| 20 | // 获取NetworkInfo对象 |
| 21 | NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); |
| 22 | |
| 23 | if (networkInfo != null && networkInfo.length > 0) { |
| 24 | for (NetworkInfo aNetworkInfo : networkInfo) { |
| 25 | // System.out.println(i + "===状态===" + networkInfo[i].getState()); |
| 26 | // System.out.println(i + "===类型===" + networkInfo[i].getTypeName()); |
| 27 | // 判断当前网络状态是否为连接状态 |
| 28 | if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) { |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * 判断WIFI是否打开 |
nothing calls this directly
no outgoing calls
no test coverage detected