Android 7.1.1中captive portal检测导致网络不可用的状态
程序员文章站
2022-04-24 20:04:58
...
Android 7.1.1后 连接wifi后 可以上网 但是网络状态显示 网络不可用 ,这是由于连接wifi后系统会自动检测 captive portal 服务器地址,由于大部分国内用户访问不到 google网站 ,所以即使可以上网 但网络状态显示不可用
在android framework源码里面我们发现服务器地址为
// Default configuration values for captive portal detection probes.
// TODO: append a random length parameter to the default HTTPS url.
// TODO: randomize browser version ids in the default User-Agent String.
private static final String DEFAULT_HTTPS_URL = "https://www.google.com/generate_204";
private static final String DEFAULT_HTTP_URL =
"http://connectivitycheck.gstatic.com/generate_204";
private static String getCaptivePortalServerHttpsUrl(Context context) {
return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTPS_URL, DEFAULT_HTTPS_URL);
}
public static String getCaptivePortalServerHttpUrl(Context context) {
return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTP_URL, DEFAULT_HTTP_URL);
}
我们用可以通过命令改数据库的默认地址
AOSP issue tracker
v2ex的generate_204网址的如下:
adb shell "settings put global captive_portal_http_url http://captive.v2ex.co/generate_204"
adb shell "settings put global captive_portal_https_url https://captive.v2ex.co/generate_204"
也可以直接用命令关闭网络检测上网
adb shell settings put global captive_portal_mode 0
在源码frameworks/base/services/core/java/com/android/server/ConnectivityService.java里面
case NetworkMonitor.EVENT_PROVISIONING_NOTIFICATION: {
final int netId = msg.arg2;
final boolean visible = (msg.arg1 != 0);
final NetworkAgentInfo nai;
synchronized (mNetworkForNetId) {
nai = mNetworkForNetId.get(netId);
}
// If captive portal status has changed, update capabilities or disconnect.
if (nai != null && (visible != nai.lastCaptivePortalDetected)) {
final int oldScore = nai.getCurrentScore();
nai.lastCaptivePortalDetected = visible;
nai.everCaptivePortalDetected |= visible;
if (nai.lastCaptivePortalDetected &&
Settings.Global.CAPTIVE_PORTAL_MODE_AVOID == getCaptivePortalMode()) {
if (DBG) log("Avoiding captive portal network: " + nai.name());
nai.asyncChannel.sendMessage(
NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT);
teardownUnneededNetwork(nai);
break;
}
updateCapabilities(oldScore, nai, nai.networkCapabilities);
}
if (!visible) {
mNotifier.clearNotification(netId);
} else {
if (nai == null) {
loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor");
break;
}
if (!nai.networkMisc.provisioningNotificationDisabled) {
mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai, null,
(PendingIntent) msg.obj, nai.networkMisc.explicitlySelected);
}
}
private int getCaptivePortalMode() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.CAPTIVE_PORTAL_MODE,
Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT);
}
我们改变 Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT默认值就可以