Android M: 无法通过程序删除WIFI接入点

10
在Android M中,我正在使用以下代码来删除当前连接的WIFI AP。
void RemoveConnectedNetwork(){
    int ID=_wifiManager.getConnectionInfo().getNetworkId();
    Log.d("test", "network id = ["+ID+"]");
    boolen ret =_wifiManager.removeNetwork(ID);
    Log.d("test", "removeNetwork return ="+ret);
    _wifiManager.saveConfiguration();
}

但是RemoveConnectedNetwork()始终返回false

尽管此API在之前的版本中运行良好。

是否有任何其他Android M API可以实现此解决方案?

谢谢。

2个回答

13

Android 6.0 中的 Wifi 管理器有些变化。

如果 WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN非零值,则任何活跃设备管理员创建的 Wi-Fi 配置都不能被用户修改或删除。

用户仍然可以创建和修改自己的 Wi-Fi 配置。

活跃的设备管理员有特权编辑或删除任何 Wi-Fi 配置,包括非其创建的配置。

请参阅此链接以获取更多详细信息:https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html


谢谢。我从Google收到了类似的回复,链接为https://code.google.com/p/android/issues/detail?id=187347 - Mohammad Khalid Ahmad

1
自 Android M 版本开始,应用程序不允许修改它们没有创建的网络。如果应用程序配置了任何网络,则该网络可以从应用程序中删除。 调用 removeNetwork(int) 后,请从 "WifiConfigManager" 检查日志,您将得到以下错误:UID(应用程序 UID)无权删除配置("wifi SSID"capabilities)。
有很多原因造成这种情况,请参考以下代码和链接以获取更多详细信息。 https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java
/**
 * Checks if |uid| has permission to modify the provided configuration.
 *
 * @param config         WifiConfiguration object corresponding to the network to be modified.
 * @param uid            UID of the app requesting the modification.
 * @param ignoreLockdown Ignore the configuration lockdown checks for connection attempts.
 */
private boolean canModifyNetwork(WifiConfiguration config, int uid, boolean ignoreLockdown) {
    // System internals can always update networks; they're typically only
    // making meteredHint or meteredOverride changes
    if (uid == Process.SYSTEM_UID) {
        return true;
    }
    // Passpoint configurations are generated and managed by PasspointManager. They can be
    // added by either PasspointNetworkEvaluator (for auto connection) or Settings app
    // (for manual connection), and need to be removed once the connection is completed.
    // Since it is "owned" by us, so always allow us to modify them.
    if (config.isPasspoint() && uid == Process.WIFI_UID) {
        return true;
    }
    // EAP-SIM/AKA/AKA' network needs framework to update the anonymous identity provided
    // by authenticator back to the WifiConfiguration object.
    // Since it is "owned" by us, so always allow us to modify them.
    if (config.enterpriseConfig != null
            && uid == Process.WIFI_UID
            && TelephonyUtil.isSimEapMethod(config.enterpriseConfig.getEapMethod())) {
        return true;
    }
    final DevicePolicyManagerInternal dpmi = LocalServices.getService(
            DevicePolicyManagerInternal.class);
    final boolean isUidDeviceOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(uid,
            DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
    // If |uid| corresponds to the device owner, allow all modifications.
    if (isUidDeviceOwner) {
        return true;
    }
    final boolean isCreator = (config.creatorUid == uid);
    // Check if the |uid| holds the |NETWORK_SETTINGS| permission if the caller asks us to
    // bypass the lockdown checks.
    if (ignoreLockdown) {
        return mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
    }
    // Check if device has DPM capability. If it has and |dpmi| is still null, then we
    // treat this case with suspicion and bail out.
    if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)
            && dpmi == null) {
        Log.w(TAG, "Error retrieving DPMI service.");
        return false;
    }
    // WiFi config lockdown related logic. At this point we know uid is NOT a Device Owner.
    final boolean isConfigEligibleForLockdown = dpmi != null && dpmi.isActiveAdminWithPolicy(
            config.creatorUid, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
    if (!isConfigEligibleForLockdown) {
        return isCreator || mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
    }
    final ContentResolver resolver = mContext.getContentResolver();
    final boolean isLockdownFeatureEnabled = Settings.Global.getInt(resolver,
            Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0;
    return !isLockdownFeatureEnabled
            && mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接