How to make the Android device hold a TCP connection to Internet without wake lock?

前端 未结 3 787
孤独总比滥情好
孤独总比滥情好 2020-12-07 23:31

I want my application to be connected to server though the mobile connection, yet allowing the device to go into sleep mode. I expect it to wake up when IP packates arrives.

3条回答
  •  感动是毒
    2020-12-08 00:22

    When you are blocked on a read from a tcp stream the device can go into a deep sleep and when tcp traffic comes in it will briefly wakeup the device, as soon as a bit is read in you start a wakelock until you have received the whole transmission then release it.

    Here is an example with web sockets, I've ran this app for over 12 hours in the background with no battery impact. https://github.com/schwiz/android-websocket-example

    The client is here, the blocking read is in the start method. https://github.com/schwiz/android-websockets/blob/master/src/com/codebutler/android_websockets/HybiParser.java

提交回复
热议问题