码迷,mamicode.com
首页 > 移动开发 > 详细

判断手机网络状态

时间:2016-09-01 17:50:38      阅读:133      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!