Search code examples
javaandroidkotlin-android-extensions

Android: How to determine Network speed in android programmatically


How to show a slow internet connection to the user when the network is connected Note: Not a network type (2G,3G,4G, WIFI)


Solution

  • Determining your Network Speed - (Slow Internet Speed)

    Using NetworkInfo class, ConnectivityManager and TelephonyManager to determine your Network Type.

    Download any file from the internet & calculate how long it took vs number of bytes in the file. ( Only possible way to determine Speed Check )

    I have tried the below Logic for my projects, You have also look into this, Hope it helps you.

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //should check null because in airplane mode it will be null
        NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
        int downSpeed = nc.getLinkDownstreamBandwidthKbps();
        int upSpeed = nc.getLinkUpstreamBandwidthKbps();