In the development, sometimes need to according to the user's current speed to do some operation, than the load, when the speed is very good, such as the connection is WiFi, we will download high-resolution pictures, conversely, when the user is 2g network, we give him to download low-resolution small map, thereby saving user traffic.
And Facebook has actually provided us with such a library, see Network-connection-class.
Use in fact super simple, first join the dependency
‘com.facebook.network.connectionclass:connectionclass:1.0.1‘
In this library, we are mainly using the Connectionclassmanager class, which has several major methods in this class.
- Getcurrentbandwidthquality () Gets the quality of the current speed, which is an enumeration type.
publicenum 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}
- Getdownloadkbitspersecond () Get download speed per second
In addition, we can get notification when the quality of the network changes, such as from poor to good, we just register the listener, this is an observer pattern.
privatenew ConnectionChangedListener();privateclass ConnectionChangedListener implements ConnectionClassManager.ConnectionClassStateChangeListener { @Override publicvoidonBandwidthStateChange(ConnectionQuality bandwidthState) { Log.e("onBandwidthStateChange", bandwidthState.toString()); }}
We register the listener in the activity's Onresume and cancel the registration in OnPause
@Override protectedvoidonResume() { super.onResume(); ConnectionClassManager.getInstance().register(listener); } @Override protectedvoidonPause() { super.onPause(); ConnectionClassManager.getInstance().remove(listener); }
Then, before the network request, we need to call the method that starts the sampling
DeviceBandwidthSampler.getInstance().startSampling();
End sampling After network request is complete
DeviceBandwidthSampler.getInstance().stopSampling();
Let's say we're using okhttp.
Okhttpclient client = new Okhttpclient ();Request. BuilderBuilder = 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 (Pager Pager, IOException e) {Devicebandwidthsampler. getinstance(). stopsampling();Log. E("TAG","OnFailure:"+E);} @Override public void Onresponse (Pager Pager, 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");} });} });
The final effect is as shown
But the library can only get download speed, upload speed is not available, when using the attention can be.
SOURCE download
http://download.csdn.net/detail/sbsujjbcy/9438965
Android Gets the current network speed Quality tuning Web request