Last Updated: February 25, 2016
·
4.298K
· iankitshah

Android: Check Network Available

Network-connected Android apps use HTTP to send and receive data, now before your app attempts to connect to the network, it should check whether a network connection is available on Android device...

1. Add "ACCESS-NETWORK-STATE" Permission and try below code block

public boolean isNetworkAvailable() {
    ConnectivityManager connectivityMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityMgr.getActiveNetworkInfo();
    /// if no network is available networkInfo will be null    
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

You can create simple utility with such methods... it will be useful for multiple apps/projects. Happy Coding!