Lets say there is two mobile A and B. Turn on the hotspot of Mobile A, and connect the Mobile B. Now turn off the Mobile Data of Mobile A. For Mobile B, there is a network available but it won't be able to connect because the Mobile Data is off in Mobile A.
How to handle this in coding?
I'm checking Internet Connection like this.
public boolean isNetworkConnected() {
boolean connected = false;
ConnectivityManager connectivityManager;
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
return connected;
}
From Activity, I'm doing like this.
if (isNetworkConnected()) {
// Connection Available
} else {
// No Connection
}
It should come in else part, but it's not coming.
You can check using InetAddress also
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
//You can replace it with your name
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}