android 中判断网络状态5G 4G 3G 还是wifi

随着5G的普及,google也在android 29(android10)之后增加判断了5G的方法.
看一下源码 TelephonyManager类:

 /** Current network is NR(New Radio) 5G. */
    public static final int NETWORK_TYPE_NR = TelephonyProtoEnums.NETWORK_TYPE_NR; // 20.

google对网络方法进一步进行了封装 TelephonyManager 类中,但google不推荐使用,注解如下(where classification is contentious, this method is conservative
但是在分类有争议的地方,这种方法是保守的):

   /**
     * @return the NETWORK_TYPE_xxxx for current data connection.
     */
    public @NetworkType int getNetworkType() {
        return getNetworkType(getSubId(SubscriptionManager.getDefaultDataSubscriptionId()));
    }

 /**
     * Return general class of network type, such as "3G" or "4G". In cases
     * where classification is contentious, this method is conservative.
     *
     * @hide
     */
    @UnsupportedAppUsage
    public static int getNetworkClass(int networkType) {
        switch (networkType) {
            case NETWORK_TYPE_GPRS:
            case NETWORK_TYPE_GSM:
            case NETWORK_TYPE_EDGE:
            case NETWORK_TYPE_CDMA:
            case NETWORK_TYPE_1xRTT:
            case NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case NETWORK_TYPE_UMTS:
            case NETWORK_TYPE_EVDO_0:
            case NETWORK_TYPE_EVDO_A:
            case NETWORK_TYPE_HSDPA:
            case NETWORK_TYPE_HSUPA:
            case NETWORK_TYPE_HSPA:
            case NETWORK_TYPE_EVDO_B:
            case NETWORK_TYPE_EHRPD:
            case NETWORK_TYPE_HSPAP:
            case NETWORK_TYPE_TD_SCDMA:
                return NETWORK_CLASS_3_G;
            case NETWORK_TYPE_LTE:
            case NETWORK_TYPE_IWLAN:
            case NETWORK_TYPE_LTE_CA:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
        }
    }

我们进行进一步封装如下:

public static String getNetWorkClass(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return Constants.NETWORK_CLASS_2_G;

            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            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 Constants.NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return Constants.NETWORK_CLASS_4_G;
           case TelephonyManager.NETWORK_TYPE_NR:
                return Constants.NETWORK_CLASS_5_G;
            default:
                return Constants.NETWORK_WIFI;
        }
    }

    class Constants {

        /**
         * wifi net work
         */
        public static final String NETWORK_WIFI = "wifi";

        /**
         * "2G" networks
         */
        public static final String NETWORK_CLASS_2_G = "2G";

        /**
         * "3G" networks
         */
        public static final String NETWORK_CLASS_3_G = "3G";

        /**
         * "4G" networks
         */
        public static final String NETWORK_CLASS_4_G = "4G";
       /**
         * "5G" networks
         */
        public static final String NETWORK_CLASS_5_G = "5G";

    }

你可能感兴趣的:(android 中判断网络状态5G 4G 3G 还是wifi)