📜  如何在 Android 中检测蜂窝网络类型(2G、3G、4G 和 5G)?(1)

📅  最后修改于: 2023-12-03 14:52:13.006000             🧑  作者: Mango

在 Android 中检测蜂窝网络类型(2G、3G、4G 和 5G)

在 Android 中,可以使用 TelephonyManager 类来检测设备当前所连接的蜂窝网络类型。

获取蜂窝网络类型

使用 TelephonyManager 类中的 getNetworkType() 方法可以获取当前网络类型的整数值。

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();
判断网络类型

根据 getNetworkType() 方法返回的整数值,可以判断当前网络类型。常用的蜂窝网络类型包括 2G、3G、4G 和 5G,它们的整数值如下表所示:

| 网络类型 | 整数值 | |---------|-------| | 未知网络 | 0 | | 2G | 1 | | 3G | 2 | | 4G | 13 | | 5G | 20 |

可以编写 is2G(), is3G(), is4G()is5G() 方法来判断当前网络类型是否为相应的类型:

private boolean is2G(int networkType) {
    return networkType == TelephonyManager.NETWORK_TYPE_GPRS || 
           networkType == TelephonyManager.NETWORK_TYPE_EDGE ||
           networkType == TelephonyManager.NETWORK_TYPE_CDMA ||
           networkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
           networkType == TelephonyManager.NETWORK_TYPE_IDEN;
}

private boolean is3G(int networkType) {
    return networkType == TelephonyManager.NETWORK_TYPE_UMTS ||
           networkType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
           networkType == TelephonyManager.NETWORK_TYPE_EVDO_A ||
           networkType == TelephonyManager.NETWORK_TYPE_HSDPA ||
           networkType == TelephonyManager.NETWORK_TYPE_HSUPA ||
           networkType == TelephonyManager.NETWORK_TYPE_HSPA ||
           networkType == TelephonyManager.NETWORK_TYPE_EVDO_B ||
           networkType == TelephonyManager.NETWORK_TYPE_EHRPD ||
           networkType == TelephonyManager.NETWORK_TYPE_HSPAP;
}

private boolean is4G(int networkType) {
    return networkType == TelephonyManager.NETWORK_TYPE_LTE;
}

private boolean is5G(int networkType) {
    // 在 API Level 29 (Android 10) 及以上版本上,可以使用下面的判断方法
    return networkType == TelephonyManager.NETWORK_TYPE_NR;
}
完整代码
import android.content.Context;
import android.telephony.TelephonyManager;

public class NetworkUtils {

    private static final int NETWORK_TYPE_UNKNOWN = 0;
    private static final int NETWORK_TYPE_2G = 1;
    private static final int NETWORK_TYPE_3G = 2;
    private static final int NETWORK_TYPE_4G = 13;
    private static final int NETWORK_TYPE_5G = 20;

    public static boolean isConnect2G(Context context) {
        int networkType = getNetworkType(context);
        return is2G(networkType);
    }

    public static boolean isConnect3G(Context context) {
        int networkType = getNetworkType(context);
        return is3G(networkType);
    }

    public static boolean isConnect4G(Context context) {
        int networkType = getNetworkType(context);
        return is4G(networkType);
    }

    public static boolean isConnect5G(Context context) {
        int networkType = getNetworkType(context);
        return is5G(networkType);
    }

    private static int getNetworkType(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getNetworkType();
    }

    private static boolean is2G(int networkType) {
        return networkType == TelephonyManager.NETWORK_TYPE_GPRS ||
                networkType == TelephonyManager.NETWORK_TYPE_EDGE ||
                networkType == TelephonyManager.NETWORK_TYPE_CDMA ||
                networkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
                networkType == TelephonyManager.NETWORK_TYPE_IDEN;
    }

    private static boolean is3G(int networkType) {
        return networkType == TelephonyManager.NETWORK_TYPE_UMTS ||
                networkType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
                networkType == TelephonyManager.NETWORK_TYPE_EVDO_A ||
                networkType == TelephonyManager.NETWORK_TYPE_HSDPA ||
                networkType == TelephonyManager.NETWORK_TYPE_HSUPA ||
                networkType == TelephonyManager.NETWORK_TYPE_HSPA ||
                networkType == TelephonyManager.NETWORK_TYPE_EVDO_B ||
                networkType == TelephonyManager.NETWORK_TYPE_EHRPD ||
                networkType == TelephonyManager.NETWORK_TYPE_HSPAP;
    }

    private static boolean is4G(int networkType) {
        return networkType == TelephonyManager.NETWORK_TYPE_LTE;
    }

    private static boolean is5G(int networkType) {
        // 在 API Level 29 (Android 10) 及以上版本上,可以使用下面的判断方法
        return networkType == TelephonyManager.NETWORK_TYPE_NR;
    }
}