zoukankan      html  css  js  c++  java
  • Android 开发工具类 18_NetWorkUtil

    检测网络的一个工具包:

    1、网络是否可用;

    2、判断是否有网络连接;

    3、判断 WIFI 网络是否可用;

    4、判断 MOBILE 网络是否可用;

    5、获取当前网络连接的类型信息;

    6、获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络;

      1 import android.content.Context;
      2 import android.net.ConnectivityManager;
      3 import android.net.NetworkInfo;
      4 
      5 /**
      6  * @Title NetWorkUtil
      7  * @Description 是检测网络的一个工具包
      8  * @author 淡定
      9  */
     10 public class NetWorkUtil {
     11     
     12     public static enum NetType {
     13         WIFI, CMNET, CMWAP, NoneNet
     14     }
     15 
     16     /**
     17      * 网络是否可用
     18      * 
     19      * @param context
     20      * @return
     21      */
     22     public static boolean isNetworkAvailable(Context context) {
     23         
     24         ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     25         NetworkInfo[] info = mgr.getAllNetworkInfo();
     26         if (info != null) {
     27             for (int i = 0; i < info.length; i++) {
     28                 if (info[i].getState() == NetworkInfo.State.CONNECTED) {
     29                     return true;
     30                 }
     31             }
     32         }
     33         return false;
     34     }
     35 
     36     /**
     37      * 判断是否有网络连接
     38      * 
     39      * @param context
     40      * @return
     41      */
     42     public static boolean isNetworkConnected(Context context) {
     43         if (context != null) {
     44             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     45             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
     46             if (mNetworkInfo != null) {
     47                 return mNetworkInfo.isAvailable();
     48             }
     49         }
     50         return false;
     51     }
     52 
     53     /**
     54      * 判断WIFI网络是否可用
     55      * 
     56      * @param context
     57      * @return
     58      */
     59     public static boolean isWifiConnected(Context context) {
     60         
     61         if (context != null) {
     62             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     63             NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
     64             if (mWiFiNetworkInfo != null) {
     65                 return mWiFiNetworkInfo.isAvailable();
     66             }
     67         }
     68         return false;
     69     }
     70 
     71     /**
     72      * 判断 MOBILE 网络是否可用
     73      * 
     74      * @param context
     75      * @return
     76      */
     77     public static boolean isMobileConnected(Context context) {
     78         if (context != null) {
     79             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     80             NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     81             if (mMobileNetworkInfo != null) {
     82                 return mMobileNetworkInfo.isAvailable();
     83             }
     84         }
     85         return false;
     86     }
     87 
     88     /**
     89      * 获取当前网络连接的类型信息
     90      * 
     91      * @param context
     92      * @return
     93      */
     94     public static int getConnectedType(Context context) {
     95         
     96         if (context != null) {
     97             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     98             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
     99             if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
    100                 return mNetworkInfo.getType();
    101             }
    102         }
    103         return -1;
    104     }
    105 
    106     /**
    107      * 获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络
    108      * 
    109      * @param context
    110      * @return
    111      */
    112     public static NetType getAPNType(Context context) {
    113         
    114         ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    115         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    116         if (networkInfo == null) {
    117             return NetType.NoneNet;
    118         }
    119         int nType = networkInfo.getType();
    120 
    121         if (nType == ConnectivityManager.TYPE_MOBILE) {
    122             if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
    123                 return NetType.CMNET;
    124             } else {
    125                 return NetType.CMWAP;
    126             }
    127         } else if (nType == ConnectivityManager.TYPE_WIFI) {
    128             return NetType.WIFI;
    129         }
    130         return NetType.NoneNet;
    131 
    132     }
    133 }

     NetworkUtil.java

     1 package com.oliver.tcpsocket;
     2 
     3 import android.content.Context;  
     4 import android.net.ConnectivityManager;  
     5 import android.net.NetworkInfo;  
     6 import android.net.wifi.WifiInfo;  
     7 import android.net.wifi.WifiManager;  
     8   
     9 public class NetworkUtil {  
    10   
    11     /** 
    12      *判断wifi是否连接  
    13      * @param context 
    14      * @return  
    15      */  
    16     public static boolean isWiFiConnected(Context context){  
    17         
    18         ConnectivityManager connectManager = 
    19                 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
    20         NetworkInfo networkInfo = 
    21                 connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
    22         if(networkInfo.isConnected()){  
    23             return true;  
    24         }  
    25         else{  
    26             return false;  
    27         }    
    28     }  
    29   
    30     /** 
    31      * 得到wifi连接的IP地址 
    32      * @param context 
    33      * @return 
    34      */  
    35     public static String getWifiIP(Context context){  
    36         
    37         WifiManager wifiManager = 
    38                 (WifiManager)context.getSystemService(Context.WIFI_SERVICE);  
    39         WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
    40         int ipAddr = wifiInfo.getIpAddress();  
    41         String ipStr =  int2string(ipAddr);  
    42         return ipStr;  
    43     }  
    44   
    45     /** 
    46      * 输入int 得到String类型的ip地址 
    47      * @param i 
    48      * @return 
    49      */  
    50     private static String int2string(int i){  
    51   
    52         return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );  
    53   
    54     }  
    55 }  
  • 相关阅读:
    Day 20 初识面向对象
    Day 16 常用模块
    Day 15 正则表达式 re模块
    D14 模块 导入模块 开发目录规范
    Day 13 迭代器,生成器,内置函数
    Day 12 递归,二分算法,推导式,匿名函数
    Day 11 闭包函数.装饰器
    D10 函数(二) 嵌套,命名空间作用域
    D09 函数(一) 返回值,参数
    Day 07 Day08 字符编码与文件处理
  • 原文地址:https://www.cnblogs.com/renzimu/p/4537936.html
Copyright © 2011-2022 走看看