Check the connectivity status in android

Many of web based applications, check the internet connections, before done the web services.

You can set the permission for network state, like this

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

Code:

public boolean networkAvailability() {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}

}
return false;
}


Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter