I am writing an app which sends some data to the server by POST
method and receives a response. To do this, I firstly need to check if network is available. I am using this method:
private boolean isNetworkAvailable() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
However, this function is not very accurate. For example, sometimes my device network indicator in status bar shows that it is currently connected to an EDGE
network or a 3G
network, but when I try to access the network, it starts changing immediately to another network type and changing back and there is no actual valid connection, but this function still returns true
. How to make sure that the network connection is valid and data can be sent through it?