网络相关辅助类 NetUtils

  1. /** 
  2.  * 跟网络相关的工具类 
  3.  *  
  4.  *  
  5.  *  
  6.  */  
  7. public class NetUtils  
  8. {  
  9.     private NetUtils()  
  10.     {  
  11.         /* cannot be instantiated */  
  12.         throw new UnsupportedOperationException("cannot be instantiated");  
  13.     }  
  14.   
  15.     /** 
  16.      * 判断网络是否连接 
  17.      *  
  18.      * @param context 
  19.      * @return 
  20.      */  
  21.     public static boolean isConnected(Context context)  
  22.     {  
  23.   
  24.         ConnectivityManager connectivity = (ConnectivityManager) context  
  25.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  26.   
  27.         if (null != connectivity)  
  28.         {  
  29.   
  30.             NetworkInfo info = connectivity.getActiveNetworkInfo();  
  31.             if (null != info && info.isConnected())  
  32.             {  
  33.                 if (info.getState() == NetworkInfo.State.CONNECTED)  
  34.                 {  
  35.                     return true;  
  36.                 }  
  37.             }  
  38.         }  
  39.         return false;  
  40.     }  
  41.   
  42.     /** 
  43.      * 判断是否是wifi连接 
  44.      */  
  45.     public static boolean isWifi(Context context)  
  46.     {  
  47.         ConnectivityManager cm = (ConnectivityManager) context  
  48.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  49.   
  50.         if (cm == null)  
  51.             return false;  
  52.         return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;  
  53.   
  54.     }  
  55.   
  56.     /** 
  57.      * 打开网络设置界面 
  58.      */  
  59.     public static void openSetting(Activity activity)  
  60.     {  
  61.         Intent intent = new Intent("/");  
  62.         ComponentName cm = new ComponentName("com.android.settings",  
  63.                 "com.android.settings.WirelessSettings");  
  64.         intent.setComponent(cm);  
  65.         intent.setAction("android.intent.action.VIEW");  
  66.         activity.startActivityForResult(intent, 0);  
  67.     }  
  68.   
  69. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值