美文网首页
Android-获取当前网络类型: 无网络、WIFI、2G、3G

Android-获取当前网络类型: 无网络、WIFI、2G、3G

作者: 晴天ccc | 来源:发表于2022-02-26 19:11 被阅读0次

    工具类

    新建工具类IntentUtil

    
    /**
     * 获取当前网络类型: 无网络、WF、2G、3G、4G、5G 网络
     */
    public class IntentUtil {
    
        public static final int NETWORK_NONE = 0; // 没有网络连接
        public static final int NETWORK_WIFI = 1; // wifi连接
        public static final int NETWORK_2G = 2; // 2G
        public static final int NETWORK_3G = 3; // 3G
        public static final int NETWORK_4G = 4; // 4G
        public static final int NETWORK_5G = 5; // 5G
        public static final int NETWORK_MOBILE = 6; // 手机流量
    
    
        /**
         * 判断网络是否连接
         *
         * @param context context
         * @return true/false
         */
        public static boolean isNetConnected(Context context) {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        /**
         * 获取运营商名字
         *
         * @param context context
         * @return int
         */
        public static String getOperatorName(Context context) {
            /*
             * getSimOperatorName()就可以直接获取到运营商的名字
             * 也可以使用IMSI获取,getSimOperator(),然后根据返回值判断,例如"46000"为移动
             * IMSI相关链接:http://baike.baidu.com/item/imsi
             */
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            // getSimOperatorName就可以直接获取到运营商的名字
            return telephonyManager.getSimOperatorName();
        }
    
        /**
         * 获取当前网络连接类型
         */
        public static int getNetworkState(Context context) {
            //获取系统的网络服务
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //如果当前没有网络
            if (null == connManager)
                return NETWORK_NONE;
            //获取当前网络类型,如果为空,返回无网络
            @SuppressLint("MissingPermission") NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
            if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
                return NETWORK_NONE;
            }
            // 判断是不是连接的是不是wifi
            @SuppressLint("MissingPermission") NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (null != wifiInfo) {
                NetworkInfo.State state = wifiInfo.getState();
                if (null != state)
                    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                        return NETWORK_WIFI;
                    }
            }
            // 如果不是wifi,则判断当前连接的是运营商的哪种网络2g、3g、4g等
            @SuppressLint("MissingPermission") NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (null != networkInfo) {
                NetworkInfo.State state = networkInfo.getState();
                String strSubTypeName = networkInfo.getSubtypeName();
                if (null != state)
                    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                        switch (activeNetInfo.getSubtype()) {
                            //如果是2g类型
                            case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
                            case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
                            case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
                            case TelephonyManager.NETWORK_TYPE_1xRTT:
                            case TelephonyManager.NETWORK_TYPE_IDEN:
                                return NETWORK_2G;
                            //如果是3g类型
                            case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                            case TelephonyManager.NETWORK_TYPE_UMTS:
                            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                            case TelephonyManager.NETWORK_TYPE_HSDPA:
                            case TelephonyManager.NETWORK_TYPE_HSUPA:
                            case TelephonyManager.NETWORK_TYPE_HSPA:
                            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                            case TelephonyManager.NETWORK_TYPE_EHRPD:
                            case TelephonyManager.NETWORK_TYPE_HSPAP:
                                return NETWORK_3G;
                            //如果是4g类型
                            case TelephonyManager.NETWORK_TYPE_LTE:
                                return NETWORK_4G;
                            case TelephonyManager.NETWORK_TYPE_NR: //对应的20 只有依赖为android 10.0才有此属性
                                return NETWORK_5G;
                            default:
                                //中国移动 联通 电信 三种3G制式
                                if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                                    return NETWORK_3G;
                                } else {
                                    return NETWORK_MOBILE;
                                }
                        }
                    }
            }
            return NETWORK_NONE;
        }
    
        /**
         * 判断是否WIFI连接
         *
         * @param context context
         * @return true/false
         */
        public static synchronized boolean isWifiConnected(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null) {
                    int networkInfoType = networkInfo.getType();
                    if (networkInfoType == ConnectivityManager.TYPE_WIFI || networkInfoType == ConnectivityManager.TYPE_ETHERNET) {
                        return networkInfo.isConnected();
                    }
                }
            }
            return false;
        }
    
    }
    
    

    调用方法

    在任意Activity中直接调用即可,可以把MainActivity更换成自己所在的Activity名称。

          Context mContext = MainActivity.this;
    
           // 网络是否连接
            boolean netConnected = IntentUtil.isNetConnected(mContext);
            // 运营商名称
            String operatorName = IntentUtil.getOperatorName(mContext);
            // 网络连接类型
            int networkState = IntentUtil.getNetworkState(mContext);
            // WIFI是否连接
            boolean wifiConnected = IntentUtil.isWifiConnected(mContext);
    
            CZLog.e("网络是否连接: " + (netConnected == true ? "YES" : "NO"));
            CZLog.e("运营商名称: " + operatorName);
            CZLog.e("网络连接类型: " + String.valueOf(networkState));
            CZLog.e("WIFI是否连接: " + (wifiConnected == true ? "YES" : "NO"));
    

    相关文章

      网友评论

          本文标题:Android-获取当前网络类型: 无网络、WIFI、2G、3G

          本文链接:https://www.haomeiwen.com/subject/jqnhrrtx.html