Android:如何以编程方式确定Android中的网络速度

时间:2019-03-14 09:55:28

标签: java android kotlin-android-extensions

如何在网络连接后向用户显示缓慢的Internet连接 注意:不是网络类型(2G,3G,4G,WIFI)

8 个答案:

答案 0 :(得分:0)

您不能直接检查它。

但是,您可以ping服务器并查看响应是否花费很长时间或根本不返回。

您也可以进行速度测试。不过,在大多数应用程序中,这并没有多大意义,因为这是对用户数据的浪费。

答案 1 :(得分:0)

您可以做的最基本的事情是探查正常运行时间几乎为100%的URL,例如www.google.com。从开始请求到完成,您都可以衡量为请求下载的数据量。这样您就拥有了数据(空间)和花费的时间。只需按时间划分空间即可获得网络速度。这有点不精确,因为它还取决于您探查的URL。

答案 2 :(得分:0)

似乎android不允许直接这样做。您可以尝试下载一些文件来确定Internet的速度。 下面是连接质量的列表:

  • POOR //带宽低于150 kbps。
  • MODERATE //带宽在150到550 kbps之间。
  • GOOD //带宽超过2000 kbps。
  • EXCELLENT //带宽超过2000 kbps。
  • 未知//连接质量找不到。

您可以在本文中查看详细信息 https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

答案 3 :(得分:0)

检查移动网络的互联网速度以使用此代码

ConnectivityManager connectivityManager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
NetworkCapabilities nc = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
var downSpeed = nc.getLinkDownstreamBandwidthKbps();
var upSpeed = nc.getLinkUpstreamBandwidthKbps();

如果检查 wifi网络的互联网速度以使用此代码

public int getWifiLevel()
{
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int linkSpeed = wifiManager.getConnectionInfo().getRssi();
    int level = WifiManager.calculateSignalLevel(linkSpeed, 5);
    return level;
}

更多详细信息,请参见此链接

https://android.jlelse.eu/designing-android-apps-to-handle-slow-network-speed-dedc04119aac

希望这对您有帮助!

谢谢。

答案 4 :(得分:0)

确定网络速度-(缓慢的Internet速度)

使用NetworkInfo类,ConnectivityManager和TelephonyManager确定您的网络类型。

从互联网上下载任何文件,并计算花费了多长时间与文件中的字节数。 (确定速度检查的唯一可行方法)

我已经为我的项目尝试了以下逻辑,您也对此进行了研究,希望它对您有帮助。

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

答案 5 :(得分:0)

您可以使用Facebook的Network Connection Class

其ConnectionClassStateChangeListener具有名为onBandwidthStateChange的方法,该方法返回 ConnectionQuality 类的实例:

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

ConnectionQuality类是如下定义的枚举类:

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
}

答案 6 :(得分:0)

以编程方式确定android中的网络速度 使用此实用程序类可能对您的查询有帮助

需要清单中的许可:

 <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
        }
    }
}

输出:

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

答案 7 :(得分:-2)

需要最低API:21。我结合使用ConnectivityManagerNetworkCapabilities来获得下行带宽和上行带宽。效果很好。您可以根据kbps的速度来确定速度是2G,3G还是4G级别。

ConnectivityManager cm = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
var downSpeed = nc.getLinkDownstreamBandwidthKbps();
var upSpeed = nc.getLinkUpstreamBandwidthKbps();
  • 2G GSM〜14.4 Kbps
  • G GPRS〜26.8 Kbps
  • E EDGE〜108.8 Kbps
  • 3G UMTS〜128 Kbps
  • H HSPA〜3.6 Mbps
  • H + HSPA +〜14.4 Mbps-23.0 Mbps
  • 4G LTE〜50 Mbps
  • 4G LTE-A〜500 Mbps

下载文件是解决方案的过度设计,因为毕竟,您对用户的互联网连接概不负责-他们(或者更确切地说,他们的服务提供商)负责!对于大多数用例,仅根据其当前状态提供信息就足够了。这使您可以快速执行操作之前检查当前链接速度。