Doing something just BEFORE wifi disconnection
<p>I understand that on a wifi network there are sudden disconnections which prevent me from sending messages to my server.</p>

<p>But sometimes there's still one last chance before the disconnection, for example if the signal is low or the user is trying to turn off the wifi. On those occasions I would like to send a logout message to my server.</p>

<p>How do I detect disconnections like those?</p>

<p>I tried to retrieve changes of connectivity by registering a broadcast listener:</p>

<pre><code>registerReceiver(this,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
...
public void onReceive(Context context, Intent intent) {
  NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
  if( (info.getState()== State.DISCONNECTING) &amp;&amp; (info.getType() == ConnectivityManager.TYPE_WIFI) ) {
    //send logout
  }
</code></pre>

<p>But it looks like at that time it's already too late. My logout message doesn't go through.</p>

<p>Is there a better way?</p>

<p>[Update 1]
I also tried:</p>

<pre><code>if( (info.getDetailedState()== DetailedState.DISCONNECTING) &amp;&amp; connectionTypeOK ) {
</code></pre>

<p>[Update 2 - SOLUTION]
The solution is, as stated below, using a combination of receiving the RSSI_CHANGED_ACTION and WIFI_STATE_CHANGED_ACTION broadcasts to monitor the signal strength and the WIFI_STATE_DISABLING events respectively. When this happens, I send my logout request. This works exactly as I needed. Thanks!!</p>
