Android - Detect if Wifi Requires Browser Login

后端 未结 5 2139
时光说笑
时光说笑 2020-12-29 07:25

My university has an open wifi access point, however it requires you to enter your e-mail before it allows you to use the web. My problem is that the Wifi is stupid in that

5条回答
  •  生来不讨喜
    2020-12-29 08:17

    Ping an external IP address (like google.com) to see if it responds.

        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("ping -c 1 " + "google.com");
            proc.waitFor();     
            int exitCode = proc.exitValue();
            if(exitCode == 0) {
                Log.d("Ping", "Ping successful!";
            } else {
                Log.d("Ping", "Ping unsuccessful.");
            }
        } 
        catch (IOException e) {}
        catch (InterruptedException e) {}
    

    The only downside is this would also indicate that a web login is required when there is simply no internet connectivity on the WiFi access point.

    @CommonsWare I believe this is a better answer than opening a UrlConnection and checking the host, since the host doesn't always change even when displaying the redirect page. For example, I tested on a Belkin router and it leaves whatever you typed in the browser as is, but still displays its own page. urlConnection.getUrl().getHost() returns what it should because of this.

提交回复
热议问题