I understand that there are sudden outages on the Wi-Fi network that prevent me from sending messages to my server.
But sometimes there is another chance before disconnecting, for example, if the signal is low or the user is trying to disable Wi-Fi. In these cases, I would like to send an exit message to the server.
How to detect outages such as?
I tried to get connection changes by registering a broadcast listener:
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) && (info.getType() == ConnectivityManager.TYPE_WIFI) ) {
But it seems that at that time it is too late. My exit message fails.
Is there a better way?
[Update 1] I also tried:
if( (info.getDetailedState()== DetailedState.DISCONNECTING) && connectionTypeOK ) {
[Update 2 - SOLUTION] The solution, as indicated below, uses a combination of RSSI_CHANGED_ACTION and WIFI_STATE_CHANGED_ACTION broadcast reception to control signal strength and WIFI_STATE_DISABLING events, respectively. When this happens, I will send an exit request. It works as I needed. Thanks!!
source share