有些 wifi 連接之后需要登錄后才能真正上網(wǎng)。需要對(duì)這類 wifi 進(jìn)行判斷哆窿。目前發(fā)現(xiàn)了三種方法:
一、Handling Network Sign-On(From Google)
Some Wi-Fi networks block Internet access until the user clicks through a sign-on page. Such sign-on pages are typically presented by using HTTP redirects. You can use getURL() to test if your connection has been unexpectedly redirected. This check is not valid until after the response headers have been received, which you can trigger by calling getHeaderFields() or getInputStream(). For example, to check that a response was not redirected to an unexpected host:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (!url.getHost().equals(urlConnection.getURL().getHost())) { // we were redirected! Kick the user out to the browser to sign on?
}
...
} finally {
urlConnection.disconnect();
}
二厉斟、http://clients3.google.com/generate_204
參考 wifi portal 檢測(wifi連接后需要登陸驗(yàn)證的網(wǎng)絡(luò)判斷)
三挚躯、ping -c 1 114.114.114.114
public boolean isNetworkOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("ping -c 1 114.114.114.114");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}