1

I couldn't produce this example which iam about to explain, so i post this here to give me more info about it.

Lets say that we want to make an http request to receive a JSON object from a server. We hold no wakelocks nor we make our app a foreground. We make the connections and we wait for our response from server.

What if the device goes to sleep at that very moment (waiting for the JSON object). Do we get the object when the device awakes or we get a timeout exception?

NOTE

Do not involve doze mode in this example. Just that device goes to sleep. Thank you

Karan Harsh Wardhan
  • 1,096
  • 9
  • 22
  • androidhttpclient was deprecated in android 6.0. avoid using it in the first place. use volley or retrofit, it can actually handle your issue. is there any reason you want to use this legacy class? – Karan Harsh Wardhan Jan 07 '19 at 11:18
  • @KaranHarshWardhan Iam using retrofit and iam asking what is going to happen –  Jan 07 '19 at 11:29
  • you've tagged it incorrectly then – Karan Harsh Wardhan Jan 07 '19 at 11:54
  • @KaranHarshWardhan do you know anything about what iam asking? –  Jan 07 '19 at 12:09
  • https://github.com/evernote/android-job - you can use this to handle android sleep & it's cutting off of network access except for periodic access in a simplified manner – Karan Harsh Wardhan Jan 07 '19 at 12:15
  • @KaranHarshWardhan You are missing the point entirely. I know how to handle this situation. I just want to know WHAT happens when the above situation occur, using ANY http client. –  Jan 07 '19 at 12:18
  • oh ok, well on newer OS'es with doze - the network access is cut off when device sleeps so unless your library handles it you will get an exception about network access not being available and return will be null - unfortunately the issue is library dependent so you need to be more specific. – Karan Harsh Wardhan Jan 07 '19 at 12:24
  • @KaranHarshWardhan ok lets say i have an older version of android with no doze mode in it, just sleep mode and lets say we use retrofit. When the connection establishes and we wait for the response object, the device goes into sleep mode and everything pauses. What will happen to the response when we wake the device? –  Jan 07 '19 at 12:29
  • retrofit times out requests when the device goes to sleep and cuts off network access(this is device dependent,some are aggressive - some do not time out til deep sleep). the response will be lost with the connection time out error and this needs to be handled separately. when device wakes up, nothing will be left – Karan Harsh Wardhan Jan 07 '19 at 12:30
  • @KaranHarshWardhan And how retrofit knows when the device goes to sleep? –  Jan 07 '19 at 12:34

2 Answers2

0

In this case, your device will got to sleep and you will get Activity Life Cycle Method Call back that is onPause() and onStop while your service will be running. This process will not make your webservice call stop. Once you get response from server your app will get call back (if you have implemented). And nothing bad will happen until you have killed your app before going to sleep. If after web service you may be accessing some variables belonging to activity that started web service. This will lead crashing.call. When you undo sleeping state you will get call back of onStart and onResume That means when your device went to sleep it did not kill your activity.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • If the device goes to sleep, everything will stop/pause even services. –  Jan 07 '19 at 11:30
0

When an Android is kept idle, the device locks and then it goes to deep sleep mode. In deep sleep mode the Android system, closes the existing network connections like TCP or UDP. If your app is connected to a server, it loses connection to the server and tries to reconnect based on the reconnect attempt methods configured for the client. But if your app is the server, all the client will lose connection to the server, and you have to start the socket again in server and try to connect once again from the clients.

From https://stackoverflow.com/a/33366487/806328

retrofit just "listens" for network connection, when it loses it - connection time out error after specified time

Karan Harsh Wardhan
  • 1,096
  • 9
  • 22
  • It says that "If your app is connected to a server, it loses connection to the server and tries to reconnect" but if the device sleeps, how the app tries to reconnect? –  Jan 07 '19 at 12:53
  • retrofit just checks for network connectivity, it is disconnected from the device state. retrofit doesn't care about your device state, only network connectivity. they are not linked concepts – Karan Harsh Wardhan Jan 07 '19 at 13:01
  • retrofit just uses the underlying httpclient to check for this. you can override the client to set the timeout values – Karan Harsh Wardhan Jan 07 '19 at 13:03