0

I have built an android application that requires continuous internet access. I want to check it continuously, not only if the device is connected to a WiFi but also that it can retrieve data (sometimes it is connected to WiFi but still has no internet access). Is there an approach to achieve this? Also will this approach be friendly for the user (will it eat up more data) ?

Mihirrai
  • 145
  • 1
  • 4
  • http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html – Blacklight Jul 09 '14 at 07:56
  • Have a look at this would help you http://stackoverflow.com/questions/6493517/android-detect-if-device-has-internet-connection – bean_droid Jul 09 '14 at 07:58

2 Answers2

0

I want to check it continuously, not only if the device is connected to a WiFi but also that it can retrieve data (sometimes it is connected to WiFi but still has no internet access). Is there an approach to achieve this?

Yes, it's possible. Code from here:

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

You can run this before making network requests.

Alternatively you can implement a BoadcastReceiver and be notified on network connection changes. You need to register for the action:

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

More info in the developer guide.

Community
  • 1
  • 1
LordRaydenMK
  • 11,579
  • 4
  • 47
  • 53
  • sometimes it is connected to WiFi but still has no internet access : This'll be a problem in the suggested approach. – Shivam Verma Jul 09 '14 at 08:01
  • I did try that. While it does confirm if the device is connected to a WiFi or not, it does not help me confirm if the WiFi has internet access. – Mihirrai Jul 09 '14 at 08:02
  • @user3615476 try connect to one reachable site, like `google`, then get response and check if is equal 200 then you access to internet, you need this answer plus my comment. that means if `netInfo != null && netInfo.isConnectedOrConnecting()` then call one site if response is equal 200 then return true, else return false – Shayan Pourvatan Jul 09 '14 at 08:04
  • @shayanpourvatan what if that particular site is not available in that country/region? – Mihirrai Jul 09 '14 at 08:06
  • @Mihirrai if you have an server you can use your site, if you don't have you can use google, I don't think so google not be available on any country/region. – Shayan Pourvatan Jul 09 '14 at 08:07
0

you can use this.

public boolean isConnected() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

isConnected used for checking connection to network, then use following code to check Internet accessibility

public boolean isOnline() {

    if (isConnected()) {
        try {
            URL url = new URL("http://www.google.com");  // or any valid link.
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                return true;
            }
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return false;
}

you can call this from server class

Shayan Pourvatan
  • 11,622
  • 4
  • 39
  • 62