15

How exactly does Android determine the difference between the following states?

  1. Has a network interface active (e.g. Wifi or 3G), but unable to access the internet.

  2. Has a network interface active, and needs to ask the user to sign in to a network on a web page.

  3. Has a network interface active and able to access the internet.

Does it perhaps send a simple HTTP request to a fixed URL (perhaps on google.com?) and check that the document returned is what it expects? If so, do we know the URL used?

3 Answers3

0

UPDATE: updated the codes to avoid NetworkOnMainThreadException

I'm currently using the following method to determine whether the device can access the internet or not:

public class NetworkConnectivityHelper {
    public void isDeviceConnectedToInternet(final Activity activity, final ResultCallback callback){

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean isDeviceConnectedToInternet = false;
                NetworkInfo activeNetworkInfo = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

                if(activeNetworkInfo != null && activeNetworkInfo.isConnected()){
                    try {
                        InetAddress.getByName("google.com").isReachable(2);
                        isDeviceConnectedToInternet = true;
                    } catch (UnknownHostException e){
                        isDeviceConnectedToInternet = false;
                    } catch (IOException e){
                        isDeviceConnectedToInternet = false;
                    }
                }

                final boolean result = isDeviceConnectedToInternet;
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.done(result);
                    }
                });

            }
        }).start();

    }


    public static abstract class ResultCallback{
        public abstract void done(boolean connected);
    }
}

call this by:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    //Populate
    NetworkConnectivityHelper networkConnectivityHelper = new NetworkConnectivityHelper();
    networkConnectivityHelper.isDeviceConnectedToInternet(activity, new NetworkConnectivityHelper.ResultCallback() {
        @Override
        public void done(boolean connected) {
            if (connected) {
                //Yey, the device is connected. 
                //Now, do some work
            }else{
                //The device is disconnected
            }
        }
    });
    ....

}

hope this helps!

PinoyCoder
  • 1,112
  • 8
  • 13
  • This will forceclose on later versions of android for using a network connection on the main thread. Also, on the wifi network I tested on that requires login, this test still returned as being connected when it shouldn't. – Nathan Schwermann Oct 28 '13 at 19:46
-1

Yes you can also send a ping :)

The order operations you should use in a small network util class would be: - Check if the interface is online - Check if its able to open a socket to Google.com or ping google.com

Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
-1

Try this:

public static boolean isNetworkAvailable(Context context)
{
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();

    return (info != null);
}
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21