判断Android设备是否连接网络

[java]  view plain copy
  1. /** 
  2.   * 判断Android客户端网络是否连接  
  3.   * @param context 
  4.   * @return 真假 
  5.   */public static boolean checkNet(Context context) {     
  6.              
  7.         try {     
  8.             ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);     
  9.             if (connectivity != null) {     
  10.                  
  11.                 NetworkInfo info = connectivity.getActiveNetworkInfo();     
  12.                 if (info != null && info.isConnected()) {     
  13.                      
  14.                     if (info.getState() == NetworkInfo.State.CONNECTED) {     
  15.                         return true;     
  16.                     }     
  17.                 }     
  18.             }     
  19.         } catch (Exception e) {     
  20.         return false;     
  21. }     
  22.         return false;     
  23.     }    

以上代码只能判断是否有可用的连接,而不能判断是否能连网

 

[java]  view plain copy
  1. * 检验网络连接 并toast提示  
  2. *   
  3. @return  
  4. */  
  5. public boolean note_Intent(Context context) {  
  6.     ConnectivityManager con = (ConnectivityManager) context  
  7.         .getSystemService(Context.CONNECTIVITY_SERVICE);  
  8.     NetworkInfo networkinfo = con.getActiveNetworkInfo();  
  9.     if (networkinfo == null || !networkinfo.isAvailable()) {  
  10.     // 当前网络不可用  
  11.         Toast.makeText(context.getApplicationContext(), "请先连接Internet!",  
  12.         Toast.LENGTH_SHORT).show();  
  13.         return false;  
  14.     }  
  15.     boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)  
  16.         .isConnectedOrConnecting();  
  17.     if (!wifi) { // 提示使用wifi  
  18.         Toast.makeText(context.getApplicationContext(), "建议您使用WIFI以减少流量!",  
  19.         Toast.LENGTH_SHORT).show();  
  20.     }  
  21.     return true;  
  22.   
  23. }  

以上转自:http://www.iteye.com/topic/1117733

 

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

http://www.cnblogs.com/codeworker/archive/2012/04/23/2467180.html
一、判断网络连接是否可用

[java]  view plain copy
  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是否打开

[java]  view plain copy
  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是否打开

[java]  view plain copy
  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网络

[java]  view plain copy
  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就可以建议下载或者在线播放。

[java]  view plain copy
  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.         }    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值