Android 如果设备休眠,HTTP 事务期间会发生什么?

Android What happens during HTTP transaction if Device sleeps?

我无法给出我要解释的这个例子,所以我 post 在这里给我更多的信息。

假设我们想要发出一个 http 请求以从服务器接收一个 JSON 对象。我们不持有唤醒锁,也不让我们的应用程序成为前台。我们建立连接并等待服务器的响应。

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?

注意

本例不涉及打瞌睡模式。只有那个设备进入睡眠状态。 谢谢

在这种情况下,您的设备将进入休眠状态,您将获得 Activity 生命周期方法回调 onPause() 和onStop 而您的服务将为 运行。此过程不会使您的网络服务调用停止。一旦您从服务器获得响应,您的应用程序将收到回调(如果您已实施)。除非你在睡觉前杀死你的应用程序,否则不会发生任何坏事。如果在 web 服务 之后,您可能正在访问一些属于 activity 的变量,这些变量启动了 web 服务。这将导致 crashing.call。当您撤消睡眠状态时,您将收到 onStart 和 onResume 的回调,这意味着当您的设备进入睡眠状态时,它不会终止您的 activity。

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.

来自

改造 "listens" 网络连接,当它丢失时 - 指定时间后连接超时错误