java Android - 无法以编程方式删除 Wifi 网络 - WifiManager 类型中的 removeNetwork(int) 方法不适用于参数(字符串)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16050122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Cant Remove Wifi Network Programatically- The method removeNetwork(int) in the type WifiManager is not applicable for the arguments (String)
提问by Bill Florentine
I'm attempting to remove my wifi network programatically - however I cannot seem to get it to remove/forget the currently connected wifi connection. This should be a pretty simple task - so I'm not sure exactly what I'm doing wrong.
我正在尝试以编程方式删除我的 wifi 网络 - 但是我似乎无法让它删除/忘记当前连接的 wifi 连接。这应该是一项非常简单的任务 - 所以我不确定我做错了什么。
I'm using the following StackOverflow post as an example:
我使用以下 StackOverflow 帖子作为示例:
How to forget a wireless network in android programmatically?
public class KillTimer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.killtimer);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.getConnectionInfo().getSSID()
wifiManager.getConnectionInfo().getNetoworkId();
wifiManager.removeNetwork(wifiConfig.networkId);
wifiManager.saveConfiguration();
}}
回答by jaga
removeNetwork()
takes only integer parameters. The networkSSID
is a string value. That's the cause for the error. I see that you are using SSID which is a string. You have to give the network id which is integer. You can try getConnectionInfo().getSSID()
and compare with your ssid, if they are same then you can try getting getConnectionInfo().getNetoworkId()
which should give the connected network's network id, which you can use to removeNetwork.
removeNetwork()
只接受整数参数。该networkSSID
是一个字符串值。这就是错误的原因。我看到您使用的是字符串 SSID。您必须提供整数网络ID。您可以尝试getConnectionInfo().getSSID()
与您的 ssid 进行比较,如果它们相同,那么您可以尝试获取getConnectionInfo().getNetoworkId()
which 应该提供所连接网络的网络 ID,您可以使用它来删除网络。
Try this:
试试这个:
public class KillTimer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.killtimer);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.removeNetwork(networkId);
wifiManager.saveConfiguration();
}}
Latest Updates As Of 10 June 2019
截至 2019 年 6 月 10 日的最新更新
There are some changes for Wifi Manager in Android 6.0 onwards.
从 Android 6.0 开始,Wifi Manager 有一些变化。
Any Wi-Fi configuration created by an active Device Owner can no longer be modified or deleted by the user if WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN is non-zero.
如果 WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN 非零,则用户无法再修改或删除由活动设备所有者创建的任何 Wi-Fi 配置。
Active Device Owners have the privilege of editing or removing any Wi-Fi configurations, including those not created by them.
活动设备所有者有权编辑或删除任何 Wi-Fi 配置,包括不是由他们创建的配置。
For more details, please refer: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
更多详情请参考:https: //developer.android.com/about/versions/marshmallow/android-6.0-changes.html
回答by hamid Hymanob
private void RemoveWifiNetworks() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
//int networkId = wifiManager.getConnectionInfo().getNetworkId();
wifiManager.removeNetwork(i.networkId);
wifiManager.saveConfiguration();
}
}