Android 10及以上如何检测网络类型?

How to detect network type in Android 10 and above?

我已经在我的项目中实现了网络类型获取,但在 Android10 中它不起作用,我试图找出解决方案但没有成功。这是我的代码,但它总是 return 空白(未知类型)

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    printLog("networkType","-"+networkType);
    switch (networkType) {
        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: {

            showToast(context, "Connection Available is 2G");
            return "2G";

        }

        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: {
           showToast(context, "Connection Available is 3G");
            return "3G";

        }
        case TelephonyManager.NETWORK_TYPE_LTE: {

            showToast(context, "Connection Available is 4G");
            return "4G";
        }
        case TelephonyManager.NETWORK_TYPE_NR:
        {
            showToast(context, "Connection Available is 5G");
            return "5G";
        }
        default: {
            showToast(context, "Not detect");
            return "";
        }
    }

要获取 android 10 或以上的网络类型,请使用 mTelephonyManager.getDataNetworkType();设置为 mTelephonyManager.getNetworkType();

这里是工作函数:

 @RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            //return TODO;
            networkType = mTelephonyManager.getDataNetworkType();
        }
    } else {
        networkType = mTelephonyManager.getNetworkType();
    }
    printLog("networkType", "-" + networkType);
    switch (networkType) {
        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: {

            showToast(context, "Connection Available is 2G");
            return "2G";

        }
        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: {
            showToast(context, "Connection Available is 3G");
            return "3G";

        }
        case TelephonyManager.NETWORK_TYPE_LTE: {

            showToast(context, "Connection Available is 4G");
            return "4G";
        }
        case TelephonyManager.NETWORK_TYPE_NR: {
            showToast(context, "Connection Available is 5G");
            return "5G";
        }
        default: {
            showToast(context, "Not detect");
            return "";
        }
    }
}