部分唤醒锁不起作用
我的应用程序具有活动
和后台服务
,必须24*7运行,
我的应用程序必须通过Wi与服务器通信-Fi
发送和接收信息。
问题:每当服务器发送任何警报时,我的应用程序都应该接收并弹出该应用程序,无论它是在前台还是后台运行,并向用户告知有关警报的信息。
因此,当设备处于活动状态时,此功能可以完美运行,但当设备进入睡眠模式时,1 或 2 分钟后,它会与服务器断开连接并停止通信。因此,为了解决这个问题,我编写了代码,将 WiFi 睡眠策略设置为 NEVER,并在后台服务的 OnCreate() 方法中获取部分锁定,并在 OnDestroy() 中释放锁定 服务的方法。现在观察一段时间它工作正常意味着5或10分钟之后它再次停止通信。
应用程序在Android 2.1上开发,并部署在支持Android 2.3版本的设备上。
我无法理解为什么部分锁定会这样,请帮助我解决这个问题。
问候, 皮克斯。
My application is having activities
and background service
which has to run 24*7,
My application has to talk to the server via Wi-Fi
to send and receive the information.
Problem: whenever any alarm send by the server my app should receive and pops up the app whether it is running in foreground or background and intimate to the user about the alarm.
So when device is in active state this feature working perfectly but when device goes to sleep mode, after 1 or 2 mins it disconnects from the server and stop communicating. so in order to resolve it I written code which will set the WiFi sleep policy to NEVER and acquire the partial lock in the OnCreate()
method of Background service and releasing the lock in OnDestroy()
method of the service.Now observation is for some time it is working fine means for 5 or 10 mins thereafter again it stop communicating.
App is developed on Android 2.1 and deployed on device supports Android 2.3 version.
I am not able to understand why partial lock behaves like this, please help me to resolve this issue.
regards,
Piks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也面临着同样的问题,最终找到了完美的解决方案。
尝试通过扩展 Application 类来获取唤醒锁:
代码:
如果您还有任何其他疑问,请告诉我。准备好帮忙了!
I was also facing the same problem and finally found the solution which works perfectly.
Try to acquire the wake lock by extending Application class:
Code:
Let me know if you have any other doubts. Ready to help!
您可能还需要一个 WifiManager.WifiLock:(
其中 wl 是您的 WifiManager.WifiLock,即 s)
You probably need a WifiManager.WifiLock, too:
(where wl is your WifiManager.WifiLock, which s)