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

android中判断网络连接是否可用

时间:2016-07-23 00:43:43      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

一、判断网络连接是否可用

 1 public static boolean isNetworkAvailable(Context context) {   
 2         ConnectivityManager cm = (ConnectivityManager) context   
 3                 .getSystemService(Context.CONNECTIVITY_SERVICE);   
 4         if (cm == null) {   
 5         } else {
 6        //如果仅仅是用来判断网络连接
 7         //则可以使用 cm.getActiveNetworkInfo().isAvailable();  
 8             NetworkInfo[] info = cm.getAllNetworkInfo();   
 9             if (info != null) {   
10                 for (int i = 0; i < info.length; i++) {   
11                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {   
12                         return true;   
13                     }   
14                 }   
15             }   
16         }   
17         return false;   
18     } 

 

二、判断GPS是否打开

 

1 public static boolean isGpsEnabled(Context context) {   
2         LocationManager lm = ((LocationManager) context   
3                 .getSystemService(Context.LOCATION_SERVICE));   
4         List<String> accessibleProviders = lm.getProviders(true);   
5         return accessibleProviders != null && accessibleProviders.size() > 0;   
6     } 

 

三、判断WIFI是否打开

 

1 public static boolean isWifiEnabled(Context context) {   
2         ConnectivityManager mgrConn = (ConnectivityManager) context   
3                 .getSystemService(Context.CONNECTIVITY_SERVICE);   
4         TelephonyManager mgrTel = (TelephonyManager) context   
5                 .getSystemService(Context.TELEPHONY_SERVICE);   
6         return ((mgrConn.getActiveNetworkInfo() != null && mgrConn   
7                 .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel   
8                 .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);   
9     } 

 

四、判断是否是3G网络

 

 1 public static boolean is3rd(Context context) {   
 2         ConnectivityManager cm = (ConnectivityManager) context   
 3                 .getSystemService(Context.CONNECTIVITY_SERVICE);   
 4         NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
 5         if (networkINfo != null   
 6                 && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {   
 7             return true;   
 8         }   
 9         return false;   
10     }  

 

五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。

 

 1 public static boolean isWifi(Context context) {   
 2             ConnectivityManager cm = (ConnectivityManager) context   
 3                     .getSystemService(Context.CONNECTIVITY_SERVICE);   
 4             NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
 5             if (networkINfo != null   
 6                     && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {   
 7                 return true;   
 8             }   
 9             return false;   
10         }

 

android中判断网络连接是否可用

标签:

原文地址:http://www.cnblogs.com/huolongluo/p/5697609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!