Android: How to determine Network speed in android programmatically

前端 未结 8 1067

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

相关标签:
8条回答
  • 2020-12-31 09:14

    The most basic thing you can do is to probe an URL whose uptime is almost 100% like, let's say, www.google.com. From the time you start the request, to the time it has completed, you can just measure the amount of data you have downloaded for the request. So you have the data (space) and time spent. Just divide space by time to get network speed. This is somewhat imprecise, because it also depend on the URL you probe.

    0 讨论(0)
  • 2020-12-31 09:25

    UPDATE - DEPRECATED LIBRARY - no longer mainteined

    original post:

    You can use Network Connection Class by Facebook.

    Its ConnectionClassStateChangeListener has a method called onBandwidthStateChange that returns an instance of ConnectionQuality class:

    public interface ConnectionClassStateChangeListener {
      public void onBandwidthStateChange(ConnectionQuality bandwidthState);
    }
    

    ConnectionQuality class is an enum class defined as below:

    public enum ConnectionQuality {
      /**
       * Bandwidth under 150 kbps.
       */
      POOR,
      /**
       * Bandwidth between 150 and 550 kbps.
       */
      MODERATE,
      /**
       * Bandwidth between 550 and 2000 kbps.
       */
      GOOD,
      /**
       * EXCELLENT - Bandwidth over 2000 kbps.
       */
      EXCELLENT,
      /**
       * Placeholder for unknown bandwidth. This is the initial value and will stay at this value
       * if a bandwidth cannot be accurately found.
       */
      UNKNOWN
    }
    
    0 讨论(0)
  • 2020-12-31 09:26

    to determine Network speed in android programmatically Use this util class may helpful to your query

    need to permission in the manifest:

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.INTERNET"/>
    
    import android.content.Context
    import android.net.ConnectivityManager
    import android.net.NetworkInfo
    import android.telephony.TelephonyManager
    public object ConnectivityUtil {
        /**
         * Get the network info
         * @param context
         * @return
         */
        fun getNetworkInfo(context: Context): NetworkInfo? {
            val cm =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            return cm.activeNetworkInfo
        }
    
        /**
         * Check if there is any connectivity
         * @param context
         * @return
         */
        fun isConnected(context: Context): Boolean {
            val info = getNetworkInfo(context)
            return info != null && info.isConnected
        }
    
        /**
         * Check if there is any connectivity to a Wifi network
         * @param context
         *
         * @return
         */
        fun isConnectedWifi(context: Context): Boolean {
            val info = getNetworkInfo(context)
            return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_WIFI
        }
    
        /**
         * Check if there is any connectivity to a mobile network
         * @param context
         *
         * @return
         */
        fun isConnectedMobile(context: Context): Boolean {
            val info = getNetworkInfo(context)
            return info != null && info.isConnected && info.type == ConnectivityManager.TYPE_MOBILE
        }
    
        /**
         * Check if there is fast connectivity
         * @param context
         * @return
         */
        fun isConnectedFast(context: Context): Boolean {
            val info = getNetworkInfo(context)
            return info != null && info.isConnected && isConnectionFast(
                info.type,
                info.subtype
            )
        }
        /**
         * Check if the connection is fast
         *
         * @param subType
         * @return
         */
        fun isConnectionFast(type: Int, subType: Int): Boolean {
            return if (type == ConnectivityManager.TYPE_WIFI) {
                true
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                when (subType) {
                    TelephonyManager.NETWORK_TYPE_1xRTT -> false // ~ 50-100 kbps
                    TelephonyManager.NETWORK_TYPE_CDMA -> false // ~ 14-64 kbps
                    TelephonyManager.NETWORK_TYPE_EDGE -> false // ~ 50-100 kbps
                    TelephonyManager.NETWORK_TYPE_EVDO_0 -> true // ~ 400-1000 kbps
                    TelephonyManager.NETWORK_TYPE_EVDO_A -> true // ~ 600-1400 kbps
                    TelephonyManager.NETWORK_TYPE_GPRS -> false // ~ 100 kbps
                    TelephonyManager.NETWORK_TYPE_HSDPA -> true // ~ 2-14 Mbps
                    TelephonyManager.NETWORK_TYPE_HSPA -> true // ~ 700-1700 kbps
                    TelephonyManager.NETWORK_TYPE_HSUPA -> true // ~ 1-23 Mbps
                    TelephonyManager.NETWORK_TYPE_UMTS -> true // ~ 400-7000 kbps
                    TelephonyManager.NETWORK_TYPE_EHRPD -> true // ~ 1-2 Mbps
                    TelephonyManager.NETWORK_TYPE_EVDO_B -> true // ~ 5 Mbps
                    TelephonyManager.NETWORK_TYPE_HSPAP -> true // ~ 10-20 Mbps
                    TelephonyManager.NETWORK_TYPE_IDEN -> false // ~25 kbps
                    TelephonyManager.NETWORK_TYPE_LTE -> true // ~ 10+ Mbps
                    TelephonyManager.NETWORK_TYPE_UNKNOWN -> false
                    else -> false
                }
            } else {
                false
            }
        }
    }
    

    Output:

    Log.d("feby","Network info : "+  getNetworkInfo(getApplicationContext()));
                //[type: MOBILE[LTE] - MOBILE, state: CONNECTED/CONNECTED, reason: connected, extra: jionet, roaming: false, failover: false, isAvailable: true]
    Log.d("feby","Is connected : "+ isConnected(getApplicationContext())); //true
    Log.d("feby","Is connected wifi : "+ isConnectedWifi(getApplicationContext())); //false
    Log.d("feby","Is connected mobile : "+ isConnectedMobile(getApplicationContext())); //true
    Log.d("feby","Is connected Fast : "+ isConnectedFast(getApplicationContext())); // true
    
    0 讨论(0)
  • 2020-12-31 09:29

    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();
    
    0 讨论(0)
  • 2020-12-31 09:29

    It seem that android doesn't allow this directly. You can try to download some file to determine the speed of your internet. Below are the list of the connection qualities:

    • POOR // Bandwidth under 150 kbps.
    • MODERATE // Bandwidth between 150 and 550 kbps.
    • GOOD // Bandwidth over 2000 kbps.
    • EXCELLENT // Bandwidth over 2000 kbps.
    • UNKNOWN // connection quality cannot be found.

    You can check detail in this article https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

    0 讨论(0)
  • 2020-12-31 09:31

    You can't directly check it.

    However, you could either ping a server and see if the response takes very long or doesn't come back at all.

    You could also do a speedtest. In most applications this would not make much sense though, because it's a hog on the user's data.

    0 讨论(0)
提交回复
热议问题