获得互联网速度(WIFI 和数据)

get internet speed (WIFI & Data)

我正在制作一个应用程序,我需要获得互联网速度!我可以使用 WifiInfo clasd 找到 wifi 速度,但我找不到任何代码或库来获得移动数据速度!
我应该使用图书馆还是 Android 中有 类 可用?

使用此代码

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();

没有 Android 库或 class 提供此功能。 wifiInfo.getLinkSpeed returns 网络的最大速度,它会不时变化。

虽然我在 FB SDK 中找到了一些可以提高互联网速度的东西。

ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();

这是 github 代码库: https://github.com/facebook/network-connection-class

很难准确确定移动速度,但您可以根据连接类型(EDGE、HSPA)估算速度:

public static boolean isConnectedFast(final Context context) {
    final NetworkInfo info = getNetworkInfo(context);
    return (info != null) && info.isConnected() && isConnectionFast(info.getType(), info.getSubtype());
}

public static boolean isConnectionFast(final int type, final int subType) {
    if (type == ConnectivityManager.TYPE_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
        /*
         * Above API level 7, make sure to set android:targetSdkVersion
         * to appropriate level to use these
         */
            case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                return true; // ~ 10+ Mbps
            // Unknown
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
        }
    } else {
        return false;
    }
}

完整代码见。