Search code examples
androidnetwork-connection

Test for internet connection in Android


I have seen all the answers here about network connectivity and they are very good. The only issue is the next use-case.

If the phone is connected to a wifi network but the router lost connection to the internet. so I thought about writing this addition to the code given around SO:

/** Google public DNS service IP address **/
public static final String GOOGLE = "http://8.8.8.8"; 

public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null) {
        return false;
    }
    if (info.isConnected()) {
        try {
            InetAddress addr = InetAddress.getByName(GOOGLE);
            return addr.isReachable(10000);
        } catch (UnknownHostException e) {
            Log.wtf("Utils", e);
        } catch (IOException e) {
            Log.wtf("Utils", e);
        }
        return false;
    } else
        return false;
    }
}

The only issue with this is that it runs on the main thread, or to state more correctly, it crashes for running on main-thread.

If I'll run it in a separate thread, it will return before getting a valid answer from the server.

Any thoughts on how to test for internet connection (as apposed to network connection)?


Solution

  • You have to split your logic to Before checking connectivity and after checking connectivity

    Then by using Handler and background thread you can test for connectivity like this:

    private static void onConnectivityCheckFinish(boolean result){
    
    }
     public static void isConnected(Context context) {
            ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo info = cm.getActiveNetworkInfo();
            if (info == null) {
    
                onConnectivityCheckFinish(false);
            }
     if (info.isConnected()) {
            final Handler handler = new Handler(
    
            );
            new Thread(){
                @Override
                public void run(){
                         try {
                            InetAddress addr = InetAddress.getByName(GOOGLE);
                            final boolean result = addr.isReachable(10000);
                            handler.post(new Runnable(){
                                @Override
                                public void run(){
                                    onConnectivityCheckFinish(result);
                                }
                            });
                         } catch (Exception e) {
                            Log.wtf("Utils", e);
                            handler.post(new Runnable(){
    
                                    @Override
                                    public void run(){
                                        onConnectivityCheckFinish(false);
                                }
    
                            });
    
    
    
                }
            }
            }.start();
    
    onConnectivityCheckFinish(false);}
     else {
         onConnectivityCheckFinish(false);
     }
    }