[英]Android - Cant Remove Wifi Network Programatically- The method removeNetwork(int) in the type WifiManager is not applicable for the arguments (String)
I'm attempting to remove my wifi network programatically - however I cannot seem to get it to remove/forget the currently connected wifi connection. 我试图以编程方式删除我的wifi网络 - 但是我似乎无法删除/忘记当前连接的wifi连接。 This should be a pretty simple task - so I'm not sure exactly what I'm doing wrong.
这应该是一个非常简单的任务 - 所以我不确定我做错了什么。
I'm using the following StackOverflow post as an example: 我使用以下StackOverflow帖子作为示例:
How to forget a wireless network in android programmatically? 如何以编程方式忘记android中的无线网络?
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();
}}
removeNetwork()
takes only integer parameters. removeNetwork()
只接受整数参数。 The networkSSID
is a string value. networkSSID
是字符串值。 That's the cause for the error. 这就是错误的原因。 I see that you are using SSID which is a string.
我看到你正在使用SSID这是一个字符串。 You have to give the network id which is integer.
您必须提供整数的网络ID。 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. 您可以尝试
getConnectionInfo().getSSID()
并与您的ssid进行比较,如果它们相同,那么您可以尝试获取getConnectionInfo().getNetoworkId()
,它应该提供连接网络的网络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
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.