Save Response to Server When Network Connection go

2019-08-04 19:17发布

问题:

I have two apps

Client App

and

Server App

in android

What i want

To check in my server app that weather client app has internet connection or not.

What i have done

I had read this post

I have used BroadcastReciever to Listen weather internet is available or not. All is well. When internet connection goes right , i am saving value online to Firebase "true"

But

When internet connection goes off ,

i am using Firebase onDisconnect() method to save ServerValue.TIMESTAMP

It works sometime in two minute but sometime it doesn't update firebase and value remains true.

Note what i want when client app is connected ,on firebase it should save true and when it is not connected it should save false . Though in my server app i will retreive those values to show to client is online or offline

is there any other technique to do such a scenario in android ?

What do you suggest any improvement in my current scenario. ?

Help will highly appreciated.

Thanks

回答1:

leave this onDisconnect(), write an API that will do nothing but will just ping the server after a fixed time continuously, let's say after each 2 seconds the API will be called(through service), so in case the net is disconnected or the cell phone is even off, since API will not respond to the server, here you will write a code in case app did't ping to the server after 2 seconds(or you can say after 5 seconds), the response(online status) should be FALSE automatically!

so in case the app is again connected to internet, since the service is running so service will update your false into TRUE again that's too simple!

i think this is your required function!