The accepted answer's EDIT shows how to check if something on the internet can be reached. I had to wait too long for an answer when this was not the case (with a wifi that does NOT have an internet connection). Unfortunately InetAddress.getByName does not have a timeout parameter, so the next code works around that:
private boolean internetConnectionAvailable(int timeOut) {
InetAddress inetAddress = null;
try {
Future future = Executors.newSingleThreadExecutor().submit(new Callable() {
@Override
public InetAddress call() {
try {
return InetAddress.getByName("google.com");
} catch (UnknownHostException e) {
return null;
}
}
});
inetAddress = future.get(timeOut, TimeUnit.MILLISECONDS);
future.cancel(true);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} catch (TimeoutException e) {
}
return inetAddress!=null && !inetAddress.equals("");
}