问题
I have an app that uses GPS for regular updates. 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.
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. However, exactly the same code running on a Google Nexus 7 running 4.4.2, and the updates keep coming through.
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.
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.
回答1:
First of all only the onPause() is called with 100% of probability by OS, onStop() and onDestroy() could be not called by OS. You should unregister in onPause(), in addition the design is wrong because to do it you have two options:
1) Use a pending intent with a broadcast receiver/intent service to manage the position;
2) Use a foreground service to install a location listener.
Activity it's not the good place to do this thing.
回答2:
Actually the simplest way of doing this seems to be a partial wake lock.
来源:https://stackoverflow.com/questions/24106387/sleep-issue-with-gps-location-updates-on-some-devices