简体   繁体   English

如何以编程方式忘记android中的无线网络?

[英]How to forget a wireless network in android programmatically?

I am working on an app which wifi , where user will be asked to enter password for the scanned network he selects, if user enters a correct password, it connects and works well. 我正在开发一个应用程序wifi,用户将被要求输入他选择的扫描网络的密码,如果用户输入正确的密码,它连接并运行良好。 But when user enters wrong password, a new network is added with that name, and will be failing to authenticate cos of wrong password, and it will be having authentication problem status. 但是当用户输入错误的密码时,会添加一个具有该名称的新网络,并且无法验证错误密码的cos,并且它将具有身份验证问题状态。

Now if user tries to again scan and select the same network, and enters correct password, it fails to connect even though password now is correct and will have disabled status, since the previous connection is still showing that authentication problem status. 现在,如果用户尝试再次扫描并选择相同的网络,并输入正确的密码,即使密码现在正确并且将具有禁用状态,它也无法连接,因为之前的连接仍然显示该身份验证问题状态。

How to solve this problem? 如何解决这个问题呢? Is there any way to forget all networks using ConnectivityManager or wifimanager? 有没有办法忘记使用ConnectivityManager或wifimanager的所有网络? Or any other solution? 或任何其他解决方案?

Yes, removeNetwork() works. 是的, removeNetwork()有效。 I used this to remove all networks. 我用它来删除所有网络。

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    wifiManager.removeNetwork(i.networkId);
    //wifiManager.saveConfiguration();  
}

wifiManager.saveConfiguration() wifiManager.saveConfiguration()

This method was deprecated in API level 26. There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically. 此方法在API级别26中已弃用。无需调用此方法 - addNetwork(WifiConfiguration),updateNetwork(WifiConfiguration)和removeNetwork(int)已自动保留配置。

https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration() https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()

The WifiManager source code, has this method: WifiManager源代码,有这个方法:

/*
 * Delete the network in the supplicant config.
 *
 * This function is used instead of a sequence of removeNetwork()
 * and saveConfiguration().
 *
 * @param config the set of variables that describe the configuration,
 *            contained in a {@link WifiConfiguration} object.
 * @hide
 */

public void forgetNetwork(int netId) {
    if (netId < 0) {
        return;
    }
    mAsyncChannel.sendMessage(CMD_FORGET_NETWORK, netId);
}

But this method is @hide , so we can't use it. 但是这个方法是@hide ,所以我们不能使用它。 But according to this comment: 但根据这个评论:

This function is used instead of a sequence of removeNetwork() and saveConfiguration() 使用此函数而不是removeNetwork()saveConfiguration()序列

You can try to use: removeNetwork() and saveConfiguration() instead. 您可以尝试使用: removeNetwork()saveConfiguration()

您可以使用removeNetwork()方法删除冗余网络连接(虽然我怀疑它们是否具有相同的netId ),然后重新添加连接以避免您遇到的问题。

wifiManager.saveConfiguration(); 

is deprectated in Android M. No longer need to call saveConfiguration as removeNetwork(int) already persist the configurations automatically. 在Android M中进行了删除。不再需要调用saveConfiguration,因为removeNetwork(int)已经自动保留配置。

https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration() https://developer.android.com/reference/android/net/wifi/WifiManager.html#saveConfiguration()

By doing this it is possible to get the list of networks configured in a list, then immediately perform the deletion and save. 通过执行此操作,可以获取列表中配置的网络列表,然后立即执行删除并保存。

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            wifiManager.removeNetwork(i.networkId);
            wifiManager.saveConfiguration();
        }

you need the especific permission. 你需要特殊的许可。

final int numOpenNetworksKept = Build.VERSION.SDK_INT >= 17
                ? Settings.Secure.getInt(resolver, Settings.Global.WIFI_NUM_OPEN_NETWORKS_KEPT, 10)
                : Settings.Secure.getInt(resolver, Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT, 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM