简体   繁体   中英

Detect and handle slow internet connection in android studio

Anyone here have the sample code on how to detect and handle slow internet connection?

For now I can detect whether internet connection is available or not using the codes below:

ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

        if (isConnected){

        } else{

              }

So, is there any method or way to detect slow internet connection?

For Mobile Data Link you can only check TelefonyManager.getNetworkType() to determine the current Mobile Data Link type. You should then approximate to actual speed by link type (ie for GPRS up to 128 kbps, for EDGE up to 236.8 kpbs, for 3G up to 2 Mbps, for HSDPA up to 7.2 Mbps). Take into consideration that this is only an approximation. Your could be connecting using HSDPA but your carrier limiting the top speed to 2 Mbps.

If you are using 8 level API an interesting feature called TrafficStats is also available. This lets you know the sent/received packets at a low level exchanged by your phone over the Mobile Data Link, which may offer just the information you where looking for (use these measurements with elapsed times and you can easily measure current/average used data link speed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM