Slow internet connection issue in android

2019-05-01 17:27发布

问题:

I'm creating an application in Android in which checks the Internet connectivity. I want to display a toast message when the Internet connection is very slow. Or when the server does not respond to a request. In this case I want to put a toast like Connection is slow !!!. Here in my code I have found the whether the internet is connected or not, but don't know how to toast the message of internet slow...

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

回答1:

  • For WiFi link speed check WifiInfo.getLinkSpeed()
  • For Mobile Data Link you can only check TelefonyManager.getNetworkType() to determine the current Mobile Data Link type. You should then aproximate to actual speed by link type (i.e. for GPRS up to 128 kbps, for EDGE up to 236.8 kpbs, for 3G up to 2 Mbps, for HDSPA up to 7.2 Mbps). Take into consideration that this is only an aproximation. Your could be conneting using HDSPA but your carrier limiting the top speed to 2 Mbps.

Now , you have to get speed and put condition whether below 100kbps , "low internet connection"