首页 > 代码库 > 判断手机网络状态

判断手机网络状态

 1 public static boolean isNetworkAvailable(Context context) { 2         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 3         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//版本大于21 4             Network[] networks = connectivityManager.getAllNetworks(); 5             NetworkInfo networkInfo; 6             for (Network mNetwork : networks) { 7                 networkInfo = connectivityManager.getNetworkInfo(mNetwork); 8                 if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { 9                     return true;10                 }11             }12         } else {13             if (connectivityManager != null) {14                 //noinspection deprecation15                 NetworkInfo[] info = connectivityManager.getAllNetworkInfo();16                 if (info != null) {17                     for (NetworkInfo anInfo : info) {18                         if (anInfo.getState() == NetworkInfo.State.CONNECTED) {19                             return true;20                         }21                     }22                 }23             }24         }25         return false;26     }

 

判断手机网络状态