Android Check Internet Access

You can use the following function to check if there is internet access on Android

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

It returns true if there is internet access. Remember to add the permission to the manifest.xml

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

 

Posted in Android, Tips and Tricks Tagged with: , ,