套接字连接需要android应用程序锁吗?
我的应用程序通过我编码的服务与服务器保持套接字连接,但是一旦手机休眠(屏幕关闭后几分钟),我无法从休眠的手机获得典型的响应。当服务接收到网络事件时,通常会播放声音。
我的问题是,我是否需要唤醒锁才能使我的应用程序功能正常。如果是这样,这个wake_lock对哪些组件有帮助?我很好奇声音和插座连接。我可以仅使用部分唤醒锁来做到这一点吗?
至于socket连接,如果是在wifi下进行的话,是否需要额外加个wifi锁?套接字连接仅包含一次小事务,那么有没有一种方法可以让套接字连接不在wifi下(即使用户启用了wifi),这样我就不必锁定wifi并浪费电力?或者套接字连接是否只能在 3g/4g 下方便地完成,或者可以从 wifi 优雅地退化到 3g/4g?
谢谢!
my app maintains a socket connection with a server through a Service I coded, but once the phone sleeps (a couple minutes after the screen turns off), I am unable to get the typical response from the sleeping phone. A sound is normally played when the Service receives a network event.
My question is, do I need a wake_lock to be able to have my app function. If so, for what components is this wake_lock helpful for? I am curious about the sound and the socket connection. Will I be able to do this with only a partial wake_lock?
As for the socket connection, will I need an additional wifi lock if it is done under wifi? The socket connection only consists of small transaction once a while, so is there a way that I can have the socket connection not under wifi (even if the user enabled wifi), so I don't have to wifi lock and waste power? or are socket connection conveniently done only under 3g/4g or can gracefully degenerate from wifi to 3g/4g?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正常?一般来说,是的。 如果您的网络连接是通过移动数据进行的,并且如果您有一个到服务器的开放套接字,并且如果该服务器向您发送数据包,那么短暂唤醒手机。然而,要完成任何严肃的工作,您需要获取一个
WakeLock
。如果上面的任何斜体 if 语句不成立,那么只要您尝试维持此连接,就需要WakeLock
。因此,请考虑切换到 C2DM 来发送通知。
部分
WakeLock
就足够了。是的。
并不真地。您请求套接字连接。您可以使用当时处于活动状态的任何网络。
如果 WiFi 断电或因其他原因变得不可用,网络连接会正常恢复到移动数据。然而,套接字连接则不然。如果您在 WiFi 上有套接字连接,并且 WiFi 断电,则您的套接字已关闭,您需要在新网络上重新打开它。
Generally, yes. If your network connection is via mobile data and if you have an open socket to a server and if that server sends packets to you, that will wake up the phone briefly. However, to do any serious work, you need to then acquire a
WakeLock
. And if any of the italicized if statements from above are not true, then you need aWakeLock
as long as you are trying to maintain this connection.Hence, please consider switching to C2DM for push notifications.
A partial
WakeLock
should suffice.Yes.
Not really. You request the socket connection. You use whatever network is active at the time.
Network connectivity gracefully falls back to mobile data if WiFi powers down or otherwise becomes unavailable. However, socket connnections do not. If you have a socket connection on WiFi, and WiFi powers down, your socket is closed, and you would need to reopen it on the new network.