Android :: How to disconnect from a wifi network?

你说的曾经没有我的故事 提交于 2019-12-21 07:54:25

问题


I have Googled and find many sites saying about 'disabling Wifi radio'. But in my case, I just want the android device to disconnect from a specific wifi network(SSID preknown) without switching OFF the WiFi radio. Please give me some insights on this issue


回答1:


Wow this shouldn't have taken a month to be answered.

Here's the easiest way that I usually use:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.disconnect();
 discon = new DisconnectWifi();
 registerReceiver(discon, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));

Where DisconnectWifi is just a small class the extends BroadcastReceiver:

  public class DisconnectWifi extends BroadcastReceiver  {

    @Override
    public void onReceive(Context c, Intent intent) {
        if(!intent.getParcelableExtra(wifi.EXTRA_NEW_STATE).toString().equals(SupplicantState.SCANNING)) wifi.disconnect();
        }
    }


来源:https://stackoverflow.com/questions/10469173/android-how-to-disconnect-from-a-wifi-network

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!