简体   繁体   English

Android - 无法以编程方式删除Wifi网络 - WifiManager类型中的方法removeNetwork(int)不适用于参数(String)

[英]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.

相关问题 PreparedStatement 类型中的 setString(int, String) 方法不适用于参数 (int, String[]) - The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, String[]) Android:Toast类型的方法makeText(Context,CharSequence,int)不适用于参数(ListViewAdapter,String,int) - Android: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (ListViewAdapter, String, int) 类型中的方法不适用于参数(int) - The method in the type is not applicable for the arguments (int) 类型为BitmapFactory的方法encodeResource(Resources,int)不适用于Android中的参数(Resources,String) - The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, String) in Android Integer类型的方法valueOf(String)不适用于参数(int) - The method valueOf(String) in the type Integer is not applicable for the arguments (int) PrintStream 类型中的方法 println(int) 不适用于参数 (String, int, int, int) - The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int) main类型的方法(double [])不适用于参数(int []) - The method (double[]) in the type main is not applicable for the arguments (int[]) ArrayBoss 类型中的方法 (int[]) 不适用于参数 () - The method (int[]) in the type ArrayBoss is not applicable for the arguments () Java Writer 错误:Writer 类型中的方法 write(char[], int, int) 不适用于 arguments (int, double, int, String) - Java Writer error: The method write(char[], int, int) in the type Writer is not applicable for the arguments (int, double, int, String) 类型X中的方法X不适用于参数(int) - The method X in the type X is not applicable for the arguments (int)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM