Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had a similar need in my project and ended up having to use both.</p> <p>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.</p> <p>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)</p> <p>So you should put both actions for the receiver in the manifest:</p> <pre><code>&lt;receiver android:name="net.moronigranja.tproxy.WifiReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="android.net.wifi.STATE_CHANGE"/&gt; &lt;action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>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:</p> <pre><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. } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload