Doing something just before turning off Wi-Fi

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) ) { //send logout } 

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!!

+4
source share
7 answers

You can try to implement the variable function "heartbeat" using WifiManager to detect changes in signal strength. Here you can find some related code, by the way.

Now, as soon as you receive an RSSI_CHANGED notification, according to the signal level, you will update the frequency of your heartbeats application on the server: if the signal is strong, you will only need to notify the server that the application is live. As soon as the signal becomes a week, however, it’s just like adrenaline kicking for a real living thing, so your application will notify the server more often. If the signal strength is restored, you will send a specific message so that the server knows that everything is in order again; if, however, the server does not receive this message for a certain period of time, and the "heartbeat" stops - your application stops notifying for this period of time - then the server registers it until it receives it from it.

+4
source

If you use TCP connections, the server should know when the session disconnects unexpectedly - it will receive an RST or FIN packet, depending on the configuration of the router between the client and the server. There is no need to do anything from the point of view of the client - TCP connections are designed in such a way that you can know when they will be interrupted.

+1
source

Why don't you have a server that regularly pings the client at regular intervals and just logs out if it does not receive a response? Trying to do this through the client side will be cumbersome.

0
source

It is best not to have any sessions at all, if possible.

Why is this a problem if the user does not log out?

0
source

Perhaps this is a long shot. But why don't you use Google push notifications to trigger activity if Wi-Fi is on. This will inform the server that the phone is "online". If this does not happen in X seconds or 1 minute, redirect it to another place.

0
source

I would execute a handler on a server that processes when the client cannot receive the message. After each message, the phone can send a message to the server, saying that it successfully received the message.

0
source

Are you looking for a good way for users to send / receive data after disconnecting?

HTML5 has local storage (with a good file size), so if a user tries to create a huge form, you first save it locally and then try to send it to the server. if this fails when the user reloads the page, you can first check if the file has any content, and if so, you can send this data, clear the content and continue accordingly.

maybe this will help you http://www.html5rocks.com/tutorials/appcache/beginner/

or check out the local storage tutorial: http://www.youtube.com/watch?v=h0uZIljjElo

With this, you could save frequent status data and change it on the fly.

and Android must also support HTML5.

0
source

Source: https://habr.com/ru/post/1346958/


All Articles