如何在Android 10及更高版本中检测网络类型?

问题描述

我在项目中实现了网络类型提取,但是在Android 10中它无法正常工作,我试图找出解决方案,但没有成功。这是我的代码,但是它总是返回空白(未知类型)

@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();

这是工作功能:

server {
    listen 80;
    listen [::]:80;
    
    server_name www.domain.com;
    
    location ~ [^\/](.*) {
        proxy_pass http://localhost:4025;
    }

    location / {
                proxy_pass http://localhost:4033;        
    }
}