WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?

帅比萌擦擦* 提交于 2019-11-29 00:01:33

Ok, I figured it out. It turns out I was registering for the wrong intent. I should be using WifiManager.NETWORK_STATE_CHANGED_ACTION.

Here are the snippets of relevant portions of code:

mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);


public void onReceive(Context c, Intent intent) {

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {

    NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    if(NetworkInfo.State.CONNECTED.equals(nwInfo.getState())){//This implies the WiFi connection is through
        displayNetworkInfo();
    }
}

I was able to detect after adding these permissions to manifest to detect the broadcast:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 

To get the broadcast after status is being changed see this

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