简体   繁体   English

GPS位置更新在某些设备上的“睡眠”问题

[英]'Sleep' issue with GPS location updates on some devices

I have an app that uses GPS for regular updates. 我有一个使用GPS进行定期更新的应用。 It used to be the case that when the device was put to sleep (screen turned off, screen timeout etc), the location updates kept on coming through. 过去,设备进入睡眠状态(屏幕关闭,屏幕超时等)时,位置更新一直持续进行。 This was on an HTC Desire running 2.2. 这是在运行2.2的HTC Desire上进行的。

I need that behaviour for various reasons. 我出于各种原因需要这种行为。

However, now being tested on an HTC Desire S running 4.0.4, and what seems to happen is that updates continue (1/sec) for about 5 seconds after entering sleep mode, but then stop. 但是,现在正在运行4.0.4的HTC Desire S上进行测试,似乎发生的情况是,进入睡眠模式后,更新持续(1 /秒)约5秒钟,然后停止。 However, exactly the same code running on a Google Nexus 7 running 4.4.2, and the updates keep coming through. 但是,运行4.4.2的Google Nexus 7上运行的代码完全相同,并且更新不断通过。

The activity life cycle seems to be doing all the right things (onPause when entering sleep, onResume when exiting sleep), but on the Desire S, the location updates stop. 活动生命周期似乎在做所有正确的事情(进入睡眠时为onPause,退出睡眠时为onResume),但是在Desire S上,位置更新停止。

Whilst no doubt it is more power efficient to turn them off, I need them on. 毫无疑问,关闭它们会更省电,但我需要将它们打开。 The behaviour seems OS and/or device dependant. 该行为似乎取决于操作系统和/或设备。 How can I achieve that? 我该如何实现?

Edit: actually I've found that location updates do keep coming, just much less frequently than requested. 编辑:实际上,我发现位置更新确实不断出现,只是比请求少得多。

First of all only the onPause() is called with 100% of probability by OS, onStop() and onDestroy() could be not called by OS. 首先,只有OS会以100%的概率调用onPause(),而OS不会调用onStop()和onDestroy()。 You should unregister in onPause(), in addition the design is wrong because to do it you have two options: 您应该在onPause()上取消注册,此外,设计是错误的,因为这样做有两个选择:

1) Use a pending intent with a broadcast receiver/intent service to manage the position; 1)与广播接收器/意图服务一起使用待定意图来管理位置;

2) Use a foreground service to install a location listener. 2)使用前台服务来安装位置侦听器。

Activity it's not the good place to do this thing. 活动不是执行此操作的好地方。

实际上,最简单的方法似乎是部分唤醒锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM