I had a similar need in my project and ended up having to use both.
The android.net.wifi.supplicant.CONNECTION_CHANGE action sends a broadcast when the network is connected, but usually before the device has an IP address, so I needed the android.net.wifi.STATE_CHANGE action for that.
The android.net.wifi.STATE_CHANGE action receives a broadcast on disconnect only if the device is disconnecting from a network, but wifi is still enabled (when hotspot goes out of range, for example)
So you should put both actions for the receiver in the manifest:
<receiver android:name="net.moronigranja.tproxy.WifiReceiver">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
</receiver>
and you put an if to check which action is being called in the intent. Here is the onReceive method of the BroadcastReceiver in my code:
public void onReceive(Context c, Intent intent) {
if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){
boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
if(!connected) {
//Start service for disconnected state here
}
}
else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if( netInfo.isConnected() )
{
//Start service for connected state here.
}
}
}