I am having a problem with a device that is available on the Internet or not. Let me briefly explain my request with an example: I tested my application in a use case. If I have wifi on my router, but I disconnect the Ethernet cable from my router and connect my device to Wi-Fi using the following code:
private boolean checkConnection() {
boolean connected = false;
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if ((ni.getTypeName().equalsIgnoreCase("WIFI")
|| ni.getTypeName().equalsIgnoreCase("MOBILE"))
& ni.isConnected() & ni.isAvailable()) {
connected = true;
}
}
}
return connected;
}
I get a boolean flag to connect. This code returns the status of the switch device, since my device is connected to Wi-Fi or not, but I do not get any response from the server or host, so I need to check whether Internet access is available on my device or not. Please help me with the same. I tried many approaches or methodologies, but could not succeed.
Thanks in advance.