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!
Written by Ankit Shah
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Android
Authors
Related Tags
#android
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#