How to check the internet connectivity within the network in Android (using internet of some other device through HOTSPOT)
Asked Answered
R

2

18

I have a requirement where I want to check whether there is any internet connectivity when I am connected with the network.

For example, I have device A and device B.

Device A is connected with hotspot with that of device B. In device A, I get it as connected with Wi-Fi and in device B - one device connected with hotspot.

Now, if I remove the internet from device B (not the tethering hotspot), then in device A, it still shows - connected with Wi-Fi but there is no internet connectivity.

Classes like ConnectivityManager help in determining whether a device is connected with the network not about the internet connectivity.

I want to track this issue. Is there any way to achieve this?

Restore answered 26/9, 2014 at 9:12 Comment(6)
+1, nice question, I am checking internet connectivity in one of my apps,but have not handled this issue.Curious about the answers nowLayette
B device is hotspot for device A? and once u close hotspot of device B u wanna detect that in device A right?Dogtrot
Not to remove the hotspot. Remove internet from device BRestore
What about checking connectivity with pinging www.google.com :) Is that a ridiculous solution ?Neurasthenia
i have created a request to my server, and checked if it was successful or not. could not think of any other solution.Varien
@BatuhanC: Its not a ridiculous solution. But I want to know does android provide anything from its end?Restore
P
4

It can be a ridiculous solution but i think also it could be real solution:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return new Boolean(true);
            }
        } catch (MalformedURLException mue) {
            // TODO Auto-generated catch block
            mue.printStackTrace();
        } catch (IOException ie) {
            // TODO Auto-generated catch block
            ie.printStackTrace();
        }
    }
    return false;
}
Paragon answered 26/9, 2014 at 9:37 Comment(7)
So if Google is offline that means that I don't have Internet?Dentate
I think so. It is like try to connect your server. If not check for google. If google does not connect, means you are offline.Restore
@PedroOliveira good point, but OP is going to use their own site, and you could add a couple of checks: If google is down try OP's site, then try SO or and so on... If all of those fail it's offline. It's not ideal, but I can't find anything in android that isn't ConnectivityManager related.Ivanovo
Mee too. I was looking for something which android could provide. There are so many methods which provide a check for network connectivity but none for internet within networkRestore
I wish android system can control this but i do not know about if system provides that.Neurasthenia
It will be the same behavior with wi-fi as well, right?Restore
This code block each controls network state and connection to google.Neurasthenia
C
-2

Try the below function to check your internet connection:

    public static boolean isInternetConnected(Context mContext) {

        try {
            ConnectivityManager connect = null;
            connect = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connect != null) {
                NetworkInfo resultMobile = connect
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                NetworkInfo resultWifi = connect
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

                if ((resultMobile != null && resultMobile
                        .isConnectedOrConnecting())
                        || (resultWifi != null && resultWifi
                                .isConnectedOrConnecting())) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

Add the following permissions to your manifest file,

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Contrariety answered 26/9, 2014 at 9:17 Comment(1)
Please read the question clearly user is asking about solution with ConnectivityManager but to know about internet availability not about network availability.Quasi

© 2022 - 2024 — McMap. All rights reserved.