如何让 Android 设备在没有唤醒锁定的情况下保持与 Internet 的 TCP 连接? [英] How to make the Android device hold a TCP connection to Internet without wake lock?

查看:21
本文介绍了如何让 Android 设备在没有唤醒锁定的情况下保持与 Internet 的 TCP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序通过移动连接连接到服务器,同时允许设备进入睡眠模式.我希望它在 IP 数据包到达时唤醒.

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.

这是怎么做到的?如何在不耗尽电池的情况下接收来自互联网的中断"?

How can this be done? How to receive "interrupts" from the Internet without draining battery?

推荐答案

当你从 tcp 流读取时被阻塞,设备可以进入深度睡眠,当 tcp 流量进来时,它会短暂唤醒设备,如一旦读取到位,您就会启动唤醒锁,直到您收到整个传输然后释放它.

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.

这是一个使用网络套接字的示例,我在后台运行了这个应用程序超过 12 小时,没有电池影响.https://github.com/schwiz/android-websocket-example

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

客户端来了,阻塞读在start方法里.https://github.com/schwiz/android-websockets/blob/master/src/com/codebutler/android_websockets/HybiParser.java

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

这篇关于如何让 Android 设备在没有唤醒锁定的情况下保持与 Internet 的 TCP 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆