1

I have implemented a method to check for internet connection

public boolean isInternetWorking() {
  boolean success = false;
  try {
    URL url = new URL("https://google.com");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(10000);
    connection.connect();
    success = connection.getResponseCode() == 200;
  } catch (IOException e) {
    e.printStackTrace();
  }
  return success;
}

The code works fine. However, I came across many cases where my app would not work properly due to slow internet connection.

Is there any way to show an alert message when the internet connection is slow and the data takes a long time to load?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
mrnobody
  • 409
  • 2
  • 8
  • 16
  • download a picture from google every time to check the internet. then calculate the speed. then if it goes below your threshold value then you show the slow internet connection dialog. – Sagar Nayak Jul 01 '16 at 05:09
  • No, there won't be any direct way to get internet speed in android. But you can do one thing, write code that downloads one ~50kB image from internet and calculate the taken time, based on that you can determine if the internet is slow. – Apurva Jul 01 '16 at 05:09
  • @jankigadhiya OP's already setting timeout. What he wants is to check internet speed before making api call. – Apurva Jul 01 '16 at 05:11
  • connection.setConnectTimeout(10000); remove this line and it work – mitesh viradiya Jul 01 '16 at 05:26
  • You can try this [link](http://stackoverflow.com/questions/4127836/how-to-detect-internet-connection-speed-with-java) – Sharad Gautam Jul 01 '16 at 05:29

2 Answers2

1

Facebook has released a library for checking connection, Network Connection Class
Here the excerpt:

Network Connection Class is an Android library that allows you to figure out the quality of the current user's internet connection. The connection gets classified into several "Connection Classes" that make it easy to develop against. The library does this by listening to the existing internet traffic done by your app and notifying you when the user's connection quality changes. Developers can then use this Connection Class information and adjust the application's behaviour (request lower quality images or video, throttle type-ahead, etc).

Or maybe you can use Speed Test library

Read the near similar QA:

Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0
  • 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"

Touhidur
  • 205
  • 3
  • 8