Detect network connection type on Android

前端 未结 14 1161
后悔当初
后悔当初 2020-11-22 05:55

How do you detect the network connection type on Android?

Is it through ConnectivityManager.getActiveNetworkInfo().getType(), and is the answer limited

14条回答
  •  灰色年华
    2020-11-22 06:29

    You can try this:

    public String ConnectionQuality() {
        NetworkInfo info = getInfo(context);
        if (info == null || !info.isConnected()) {
          return "UNKNOWN";
        }
    
        if(info.getType() == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            int numberOfLevels = 5;
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
            if(level == 2 )
                    return "POOR";
                else if(level == 3 )
                    return "MODERATE";
                else if(level == 4 )
                    return "GOOD";
                else if(level == 5 )
                    return "EXCELLENT";
                else
                return "UNKNOWN";
            }else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
                int networkClass = getNetworkClass(getNetworkType(context));
                if(networkClass == 1)
                    return "POOR";
                else if(networkClass == 2 )
                    return "GOOD";
                else if(networkClass == 3 )
                    return "EXCELLENT";
                else
                    return "UNKNOWN";
            }else
                return "UNKNOWN";  
    }
    
    public NetworkInfo getInfo(Context context) {
        return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    }
    
    public int getNetworkClass(int networkType) {
        try {
          return getNetworkClassReflect(networkType);
        }catch (Exception ignored) {
        }
    
        switch (networkType) {
          case TelephonyManager.NETWORK_TYPE_GPRS:
          case 16: // TelephonyManager.NETWORK_TYPE_GSM:
          case TelephonyManager.NETWORK_TYPE_EDGE:
          case TelephonyManager.NETWORK_TYPE_CDMA:
          case TelephonyManager.NETWORK_TYPE_1xRTT:
          case TelephonyManager.NETWORK_TYPE_IDEN:
            return 1;
          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:
          case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
            return 2;
          case TelephonyManager.NETWORK_TYPE_LTE:
          case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
            return 3;
          default:
            return 0;
        }
    }
    
    private int getNetworkClassReflect(int networkType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {  
        Method getNetworkClass = TelephonyManager.class.getDeclaredMethod("getNetworkClass", int.class);
        if (!getNetworkClass.isAccessible()) {
          getNetworkClass.setAccessible(true);
        }
        return (Integer) getNetworkClass.invoke(null, networkType); 
    }
    
    public static int getNetworkType(Context context) {
        return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType();
    }
    

提交回复
热议问题