想必大家都连接过那些需要验证才能使用的wifi热点吧,当你们连接这些热点的时候,android会自动弹出提示询问你是否需要登录。而这个功能就是依靠了 isCaptivePortal() 这个函数才得以实现,具体原理如下:
安卓先访问 http://clients3.google.com/generate_204 这个网址,而这个网址如字面所说,会产生一个 http 204 返回值。204返回值的意思就是空内容。如果当前wifi是需要登录才可以连接,那么当试图访问google的服务器的时候,wifi的验证机制一定会自动跳转到一个登录页面,这个时候http请求的返回值就必然不是204了。就是通过这一机制,便可以区分当前wifi是否需要验证,不得不佩服想出这个办法的人来。
然而这就导致了如果简单的屏蔽掉这个函数的功能,那么就没有办法自动提示登录了,但是如果不屏蔽掉那么这个网址被墙掉了,因此会有一个难看的感叹号。想来想去我想到了一个曲线救国的办法,那就是我们把这个网址改成国内的网址不就可以了?我选用小米的这个地址,因为小米的这个地址网络延时是做小的,可以这样设置:
adb shell settings put global captive_portal_http_url http://connect.rom.miui.com/generate_204
adb shel settings put global captive_portal_https_url https://connect.rom.miui.com/generate_204
设置captive_portal_mode
系统默认的设置是1
adb shell settings put global captive_portal_mode 0
mode的取值如下:
- 0:彻底禁用检测(Don’t attempt to detect captive portals.)
- 1:检测到需要登录则弹窗提醒(默认值)(When detecting a captive portal, display a notification that prompts the user to sign in.)
- 2:检测到需要登录则自动断开此热点并不再自动连接(When detecting a captive portal, immediately disconnect from the network and do not reconnect to that network in the future.
删除配置
adb shell settings delete global captive_portal_mode 0