Android - Sign into Wi-Fi network

Ever been notified of a WiFi login action to access internet via Android?



    The underlying infrastructure to enforce the sign in policy before having access to internet has been in place for ages and is not something specific to Android. In networking terminology, this is called a network with a Captive portal where in any access to internet is denied and the users are redirected to a captive portal either to sign in with a room number and name in a Hotel WiFi or accept terms and conditions of a Starbucks WiFi or any other policy managed by the WiFi administrators. In old days, this wasn't a big deal as user's attempt to access internet via browsers is any ways redirected to the portal. This was good enough. However, with new technologies, applications running in the background like a weather application or a stock ticker in Mac OS's dashboard needed internet access as soon as the device is connected to a WiFi. They don't have a smart logic to detect captive portals and warn users. Third party application developers simply shouldn't have to handle this case. Fortunately, operating system developers have started handling this case and Android is no exception. This is more relevant in case of a Mobile operating systems with tons of applications having background services using the internet.

    Android's solution is to thrown in a Notification warning the users of the captive portal. How does it do this? Well, it just tries to access a URL as soon as the network is connected to WiFi and waits for a http response code. A response code greater than or equal to 200 and less than or equal to 399 indicates a captive portal and servers as a entry point for Android's notification. Android just uses a SDK api, HttpURLConnection.

     public int getResponseCode ()
     Returns the response code returned by the remote HTTP server.