1. 程式人生 > >Android獲取當前網速質量——分四個等級

Android獲取當前網速質量——分四個等級

在開發中,有時候常常需要根據使用者當前的網速來做一些操作,比如圖片的載入,當網速非常好的時候,比如連線的是wifi,我們就會下載高解析度的圖片,反之,當用戶使用的是2g網時,我們則給他下載低解析度的小圖,從而節省使用者流量。

而Facebook其實已經給我們提供了這麼一個庫,詳見network-connection-class

使用其實超級簡單,先加入依賴

compile 'com.facebook.network.connectionclass:connectionclass:1.0.1'
  • 1
  • 1

在該庫中,我們使用的主要是ConnectionClassManager這個類,這個類中有幾個主要的方法。

  • getCurrentBandwidthQuality() 獲得當前網速的質量,是一個列舉型別。
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 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • getDownloadKBitsPerSecond() 獲得每秒的下載速度

此外,我們可以獲得網路質量發生改變時的通知,比如從POOR變成了GOOD,我們只要註冊監聽器即可,這是一個觀察者模式。

private ConnectionChangedListener listener = new ConnectionChangedListener();

private class ConnectionChangedListener implements
        ConnectionClassManager.ConnectionClassStateChangeListener {
    @Override
    public void onBandwidthStateChange(ConnectionQuality bandwidthState) {
        Log.e("onBandwidthStateChange", bandwidthState.toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

我們在Activity的onResume中註冊監聽器,在onPause中取消註冊

    @Override
    protected void onResume() {
        super.onResume();
        ConnectionClassManager.getInstance().register(listener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ConnectionClassManager.getInstance().remove(listener);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

之後,在網路請求之前,我們需要呼叫開始取樣的方法

 DeviceBandwidthSampler.getInstance().startSampling();
  • 1
  • 1

網路請求完成後,結束取樣

DeviceBandwidthSampler.getInstance().stopSampling();
  • 1
  • 1

假設我們使用的是OkHttp

OkHttpClient client = new OkHttpClient();
                Request.Builder builder = new Request.Builder();
                Request request = builder.url("http://www.baidu.com").build();


                DeviceBandwidthSampler.getInstance().startSampling();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        DeviceBandwidthSampler.getInstance().stopSampling();
                        Log.e("TAG","onFailure:"+e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        DeviceBandwidthSampler.getInstance().stopSampling();
                        Log.e("TAG","onResponse:"+response);
                        final ConnectionQuality connectionQuality = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
                        final double downloadKBitsPerSecond = ConnectionClassManager.getInstance().getDownloadKBitsPerSecond();
                        Log.e("TAG","connectionQuality:"+connectionQuality+" downloadKBitsPerSecond:"+downloadKBitsPerSecond+" kb/s");

                        tv.post(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText("connectionQuality:"+connectionQuality+"\n"+"downloadKBitsPerSecond:"+downloadKBitsPerSecond+" kb/s");
                            }
                        });
                    }
                });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

最終的效果如下圖所示

這裡寫圖片描述

但是該庫只能獲得下載速度,上傳的速度無法獲得,使用的時候注意一下即可。

下載地址:http://download.csdn.net/detail/chichengjunma/9850799