I have a question: How can you find out the quality of the Internet on your phone?

That is, for example, on the phone itself, on top, the quality of the connection is shown and there, if there is 3G internet, the ā€œ3Gā€ icon is drawn, but can you find out if there is 3g at the moment from the application?

    1 answer 1

    You can use this method to check the speed of the Internet:

    public static boolean isConnectionFast(int type, int subType){ if(type==ConnectivityManager.TYPE_WIFI){ System.out.println("CONNECTED VIA WIFI"); return true; }else if(type==ConnectivityManager.TYPE_MOBILE){ switch(subType){ case TelephonyManager.NETWORK_TYPE_1xRTT: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_CDMA: return false; // ~ 14-64 kbps case TelephonyManager.NETWORK_TYPE_EDGE: return false; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_EVDO_0: return true; // ~ 400-1000 kbps case TelephonyManager.NETWORK_TYPE_EVDO_A: return true; // ~ 600-1400 kbps case TelephonyManager.NETWORK_TYPE_GPRS: return false; // ~ 100 kbps case TelephonyManager.NETWORK_TYPE_HSDPA: return true; // ~ 2-14 Mbps case TelephonyManager.NETWORK_TYPE_HSPA: return true; // ~ 700-1700 kbps case TelephonyManager.NETWORK_TYPE_HSUPA: return true; // ~ 1-23 Mbps case TelephonyManager.NETWORK_TYPE_UMTS: return true; // ~ 400-7000 kbps // NOT AVAILABLE YET IN API LEVEL 7 case Connectivity.NETWORK_TYPE_EHRPD: return true; // ~ 1-2 Mbps case Connectivity.NETWORK_TYPE_EVDO_B: return true; // ~ 5 Mbps case Connectivity.NETWORK_TYPE_HSPAP: return true; // ~ 10-20 Mbps case Connectivity.NETWORK_TYPE_IDEN: return false; // ~25 kbps case Connectivity.NETWORK_TYPE_LTE: return true; // ~ 10+ Mbps // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: return false; default: return false; } }else{ return false; } } 

    Here, the input parameters are int type — the value returned by the getType() method of the getType() class, and int subType is the value returned by the getSubtype() method of the same class. However, it should be noted that the getType() method is deprecated in API level 28, and instead you should use the NetworkCapabilities.hasTransport(int) method, which accepts one of the TRANSPORT_CELLULAR, TRANSPORT_WIFI, TRANSPORT_BLUETOOTH, TRANSPORT_ETHERNET, TRANSPORT_VPN, TRANSPORT_WIFI_AWARE NetworkCapabilities.hasTransport(int) , and your NetworkCapabilities.hasTransport(int) , and the NetworkCapabilities.hasTransport(int) .

    With this in mind, an appeal to the isConnectionFast method can be made like this:

     public static boolean isConnectedFast(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = cm.getAllNetworks(); NetworkInfo networkInfo; for (Network mNetwork : networks) { networkInfo = cm.getNetworkInfo(mNetwork); if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(mNetwork); if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { isConnectionFast(NetworkCapabilities.TRANSPORT_WIFI, networkInfo.getSubtype()); } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { isConnectionFast(NetworkCapabilities.TRANSPORT_CELLULAR, networkInfo.getSubtype()); } return true; } } } else { if (cm != null) { //noinspection deprecation NetworkInfo[] info = cm.getAllNetworkInfo(); if (info != null) { for (NetworkInfo networkInfo : info) { if (networkInfo.getState() == NetworkInfo.State.CONNECTED) { return (networkInfo != null && networkInfo.isConnected() && isConnectionFast(networkInfo.getType(), networkInfo.getSubtype())); } } } } } return false; } 
    • hmm, and this whole thing is checked in a cycle? Or only at start once? - user300058
    • @ user300058, it depends on the requirements of your application. But I think that, most likely, it is enough to do the check once, if you need to "find out if there is 3g at the moment from the application". - Ksenia
    • Thank you very much! - user300058