Android 网络质量 + 下载测速 (facebook/network-connection-class 使用)

Android 网络质量 + 下载测速

facebook/network-connection-class使用

一、facebook/network-connection-class源码地址

facebook/network-connection-class gitHub地址

二、使用方式

添加依赖

// 网络测速
compile 'com.facebook.network.connectionclass:connectionclass:1.0.1'

Java代码

import com.facebook.network.connectionclass.ConnectionClassManager;
import com.facebook.network.connectionclass.ConnectionQuality;
import com.facebook.network.connectionclass.DeviceBandwidthSampler;

public class NetSpeedUtil {
    private static final String TAG = "NetSpeedUtil";

    /**
     * 开始采样(添加到网络请求开始前)
     */
    public static void startSampling() {
        DeviceBandwidthSampler.getInstance().startSampling();
    }

    /**
     * 结束采样(添加到网络请求结束后)
     */
    public static void stopSampling() {
        // Do some downloading tasks
        DeviceBandwidthSampler.getInstance().stopSampling();
    }

    /**
     * 当前网络质量
     * 

* POOR: Bandwidth under 150 kbps. * MODERATE: Bandwidth between 150 and 550 kbps. * GOOD: Bandwidth between 550 and 2000 kbps. * EXCELLENT: Bandwidth over 2000 kbps. * UNKNOWN: Placeholder for unknown bandwidth. This is the initial value and will stay at this value if a bandwidth cannot be accurately found. */ public static String getCurrentBandwidthQuality() { PalLog.d(TAG, "---getCurrentBandwidthQuality---"); ConnectionQuality currQuality = ConnectionClassManager.getInstance().getCurrentBandwidthQuality(); PalLog.d(TAG, "currQuality: " + currQuality); if (currQuality != null) { return currQuality.toString(); } return "unknown"; } /** * 当前网络质量 */ public static double getDownloadKBitsPerSecond() { PalLog.d(TAG, "---getDownloadKBitsPerSecond---"); double bitsPerSecond = ConnectionClassManager.getInstance().getDownloadKBitsPerSecond(); PalLog.d(TAG, "DownloadKBitsPerSecond: " + bitsPerSecond); return bitsPerSecond; } }

你可能感兴趣的:(Android源码)