Leafee98's Blog

修改 Android 探测网络状态所用的服务提供者

· 331 words · 1 minutes to read

Android 会间歇探测网络状态,途径为访问一个 URL,能拿到预期的响应就认为网络正常,一般使用 204 (no content) 状态码作为标准的响应,其他响应可能也会接受(存疑)

如果需要覆盖 Android 默认使用的探测服务提供者(一般是 Google),用到的命令类似下面这样(来源参考 2)

settings put global captive_portal_mode 1
settings put global captive_portal_use_https 1
settings put global captive_portal_http_url http://connect.rom.miui.com/generate_204
settings put global captive_portal_https_url https://connect.rom.miui.com/generate_204
settings put global captive_portal_fallback_url http://captive.v2ex.co/generate_204
settings put global captive_portal_other_fallback_urls http://www.google.cn/generate_204

captive_portal_mode 为 0 表示禁止探测网络状态,为 1 表示当网络需要登录时提示用户,为 2 表示当网络需要登录时断开此网络并不再连接到此网络;其他的几个变量的名字具有良好的自解释性,不再赘述(详细的解释见参考 1)。

一些公共的网络连通性测试服务见参考 4

参考:

  1. Captive Portal parameters — answered by Andy Yan
  2. chenshaoju/script running adb shell
  3. Change the server that Android uses for an internet connectivity check?
  4. 检测网络联通性&generate_204服务汇总与评测 — 李德银’s blog

Categories