首页 > 移动平台 > 详细

判断手机网络状态

时间:2016-09-01 17:48:54      阅读:211      评论:0      收藏:0      [点我收藏+]
 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 deprecation
15                 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     }

 

判断手机网络状态

原文:http://www.cnblogs.com/liuqingzi/p/5830543.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!