Save Response to Server When Network Connection goes OFF

允我心安 提交于 2019-12-24 13:33:46

问题


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!



来源:https://stackoverflow.com/questions/54834847/save-response-to-server-when-network-connection-goes-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!