Search code examples
javaandroidconnectioninetaddressandroid-connectivitymanager

find if there is an active internet connection java android


I'm trying to find out if there is an active internet connection in my app. I need to know if I can access the network without any errors, sometimes I get "true" response even though I have not internet connection and that is because i'm still connected to the WiFi but there is no internet connectivity.

At the mean time, my internet check function is:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

This function only check if i'm connected to a certain WiFi or mobile network without actually checking if I can access the network.

So I've added the next function:

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        return !ipAddr.equals("");
    } catch (Exception e) {
        return false;
    }
}

I don't know why, but this function always thinks that my internet connection is okay even though my router is not connected (I have this little yellow triangle on the network bar that says connected but no internet connection).

I tried printing everytime I call this function if the connection is true/false, and I get true at the time that i'm connected, I get false for 5-10 seconds right after I unplug my router, and then i'm getting true again... (without conencting to other WiFi network)

I've tried to test this on the ADB emulator and also on actual device, both of them crashed because I tried to access the network even though I was "offline".

Seeking for help! Thank you very much.


Solution

  • instead of isConnected() method

       return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    

    try this

    return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
    

    this is the utility class I usually use:

    public class NetworkHelper {
    
        //method that checks for network status
        public static boolean hasNetworkAccess(Context context){
    
            ConnectivityManager cm =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            try {
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                return activeNetwork !=null && activeNetwork.isConnectedOrConnecting();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
        }
    
    }
    

    Make sure you have permissions

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    

    in your activity make a

    private boolean networkOk;
    

    then use it to see if your connection is ok ex

        networkOk = NetworkHelper.hasNetworkAccess(this);
        if (networkOk) {
            Intent intent = new Intent(MainActivity.this, Myservice.class);
            intent.setData(Uri.parse(jsonurl));
            startService(intent);
        }