Getting Connection Returns False Even When The Device Has Connection
Solution 1:
This might be happening because the device have some sort of battery management system, which disables internet connection, and enables it when you open the browser, but it doesn't work when this Alarm is triggered.
Solution 2:
I was testing a lot and I found that many times when I'm asking for connection the NetworkState is "Connecting" so I change the method where I ask for connection.
publicbooleanhasInternetConnection() {
ConnectivityManagerconnectivityManager= (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfonetworkInfo= connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting())
{
returntrue;
} else {
returnfalse;
}
} else {
returnfalse;
}
}
I read the documentation on Google's developer page and found that it is not recommended to use the isConnectedOrConnecting() method if you're sending or receiving data from the internet, but I think that the BatteryManagment of some devices maybe cut the connection or maybe it doesn't get connected because the 3G network in my city sucks.
I know this is not the best practice but is working, and it's strangely sending and receiving data even when the network state is "connecting".
Post a Comment for "Getting Connection Returns False Even When The Device Has Connection"