如何在Android中断开与wifi的连接

时间:2017-09-19 01:12:02

标签: android android-wifi wifimanager

我正在试图制作一个简单的应用程序,只是断开实际的wifi并且无法重新接收。这是我的代码

public class MainActivity extends Activity {

    String networkSSID = "mySsid";
    String networkPass = "myNetPass";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";  
        conf.preSharedKey = "\""+ networkPass +"\"";
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        wifiManager.addNetwork(conf);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
               wifiManager.disconnect();
               wifiManager.disableNetwork(i.networkId);
               break;
            }
        }
    }
}

代码工作,没有错误,但我的wifi仍然连接,我该怎么办?

0 个答案:

没有答案