Android – check if the device is connected to internet.

If you want to check if your device is connected to the internet, you would need to have the permission below, to check the network statue.

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

Then you create a ConnectivityManager Object to retrieve Network information.

public static boolean IsConnectedToInternet(){
        boolean result = false;
        ConnectivityManager ConnectionInfo = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = ConnectionInfo.getActiveNetworkInfo();
        if (info!=null && info.isConnected()){
            result = true;
        }
        return result;
    }

Hope you find it useful. If you have a better way to check if the device is connected to the internet. Please leave me a message, so that I can improve my code =).

Happy coding.

Leave a Reply