i am working on app that must have an internet connection before populating data from the server . I have a bool method that check if the there is a network connection available but it is not working properly.
Here is what is what to Achieve:
1: Check if Wifi Network is Available and there is a network Access
2: Check is the Mobile Data is Enable and there is a Network Access
Problem(Bug) :
Currently, i do not have an internet access but my Mobile data is ON , instead of getting a Toast message that i am not connected to the internet, it will still tell me that i have an internet access but i cannot access the internet.
I have check every other possible means but they all have an approach related to the method i am using.
QUESTION: How can i test if there is an Active Internet Access even if the Mobile Data is Enable .
MY CODE:
private boolean haveConnected() {
//Vars
boolean wifiIsConnected = false;
boolean mobileDataIsConnected = false;
State checkMobile;
State checkWifi;
ConnectivityManager connectManager;
//init
connectManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
checkMobile = connectManager.getNetworkInfo(0).getState();
checkWifi = connectManager.getNetworkInfo(1).getState();
if (checkMobile == NetworkInfo.State.CONNECTED) {
mobileDataIsConnected = true;
} else if (checkWifi == NetworkInfo.State.CONNECTED) {
wifiIsConnected = true;
}
return wifiIsConnected || mobileDataIsConnected;
}