检查 wifi 助手是否处于活动状态(wifi 看门狗打开)

Check if wifi assistant is active (wifi watchdog on)

我一直在开发一些连接到外部设备的应用程序,该设备是一个没有网络连接的 WiFi 接入点,以便对其进行配置。

在某些手机上,这会给我带来问题,因为它们激活了“Wifi 助手”,可以在 WiFi 信号较弱时切换网络。所以我想检测这个选项是否被激活并向用户显示一个正确的对话框说他应该禁用 wifi 助手(看门狗)才能正确配置设备。

我的代码如下:

/**
 * Checks whether the "Avoid poor networks" setting (named "Auto network switch" on some Samsung devices
 * or "Wifi assistant" on some Xiaomis) is enabled, which can in some instances interfere with Wi-Fi.
 *
 * @return true if the "Avoid poor networks" or "Auto network switch" setting is enabled
 */
public static boolean isPoorNetworkAvoidanceEnabled(final Context ctx, final boolean isWatchdogServiceFind) {
    final int SETTING_UNKNOWN = -1;
    final int SETTING_ENABLED = 1;
    String avoidPoorText = "wifi_watchdog_poor_network_test_enabled";
    final ContentResolver cr = ctx.getContentResolver();

    if (isWatchdogServiceFind) {
        avoidPoorText = "wifi_watchdog_on";
    }

    int result = Settings.Global.getInt(cr, avoidPoorText, SETTING_UNKNOWN);

    // Return if the setting value is known or unknown
    if (result != SETTING_UNKNOWN) {
        return (result == SETTING_ENABLED);
    } else {
        return false;
    }
}

此代码在我的三星设备上运行良好,但 Settings.Global.getInt(cr, "wifi_watchdog_poor_network_test_enabled")Settings.Global.getInt(cr, "wifi_watchdog_on") 结果 是 returning SettingNotFoundException 对于其他制造商。至少在:小米红米7、小米红米Note 8、摩托罗拉一动

如果我 enable/disable “WiFi 助手”选项,为什么某些设备没有 return 设置值?有什么办法可以解决吗?从官方文档 (https://developer.android.com/reference/android/provider/Settings.Global#WIFI_WATCHDOG_ON) 应该 return WiFi 看门狗的实际值。

您可以使用:

adb shell settings list settings

adb shell settings list global

adb shell settings list secure

检查终端的字符串。

在小米是:

Settings.System.getString(cr,"wifi_assistant");