Android GPS 跟踪和 WakeLock
我需要在 Android 手机上进行后台 GPS 跟踪,目前我计划有两种情况:
- 当手机连接到(汽车)充电器时,获取频繁的 GPS 位置(每分钟)。
- 当手机未连接充电器时,仅每 5-30 分钟轮询一次位置,具体取决于电池受影响的程度。
我知道我需要在服务中实现跟踪,并且可能会显示一条通知以保持服务处于活动状态。不过,我推测手机可能随时进入睡眠模式,这会停止 GPS 跟踪。
对于这些场景,在手机不进入睡眠状态且不会消耗太多电量的情况下进行连续后台 GPS 跟踪的最佳方法是什么?
到目前为止,我收集了以下信息:
对于场景 1,我'你可能需要获取一个 WakeLock,并保持 CPU 始终处于唤醒状态(但屏幕可能会休眠)。 MyTracks 应用程序似乎在 TrackRecordingService. OpenGPSTracking 似乎可以 相同。
对于场景 2,我可以使用 AlarmManager 安排每 x 分钟检查一次。我的服务需要获取临时 WakeLock,等待单个 GPS 位置,然后停止并再次释放 WakeLock。
这两种方法(永久 WakeLock 与 AlarmManager)的功耗如何比较?如果仅每 30 分钟检查一次,GPS 能否在合理的时间内接收到准确的位置?
StackOverflow 上有一些相关问题,但没有一个有令人满意的答案。一些半有用的:
I need to do background GPS tracking on an Android phone, and I'm currently planning on having two scenarios:
- When the phone is connected to a (car) charger, get frequent GPS locations (every minute).
- When the phone is not connected to a charger, only poll for a location once every 5-30 minutes, depending on how much the battery is affected.
I know that I'll need to implement the tracking in a service, and probably show a notification to keep the service alive. However, I gather that the phone might go into sleep mode at any time, which would stop the GPS tracking.
For these scenarios, what is the best way to do continuous background GPS tracking, without the phone going to sleep, and without draining too much power?
So far I gathered the following:
For scenario 1, I'll probably need a acquire a WakeLock, and keep the CPU awake all the time (but the screen may sleep). The MyTracks application seems to be doing this in TrackRecordingService. OpenGPSTracking seems to do the same.
For scenario 2, I can use AlarmManager to schedule a check every x minutes. My service will need to acquire a temporary WakeLock, wait for a single GPS location, then stop and release the WakeLock again.
How would the power usage compare for these two methods (permanent WakeLock vs AlarmManager)? Can the GPS receive an accurate location in a reasonable amount of time when only checking every 30 minutes?
There are a few related questions on StackOverflow, but none with satisfying answers. Some semi-useful ones:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论电源是否连接,您都可以使用广播接收器并执行相应的操作。
电源连接时:
在清单文件中:
电源断开时:
在清单文件中:
现在您应该使用
service
并在那里初始化您的 GPS 。连接电源后,使用GPS_Provider
,否则使用网络提供商
。如果需要,请使用wakelock
。You can use broadcast receiver if the power is connected or not and do your action accordingly.
For Power Connected:
In your manifest file:
When Power is Disconnected:
In your manifest file:
Now you should use
service
and initialize your GPS there. When power is connected useGPS_Provider
else usenetwork provider
.Usewakelock
if needed.当手机连接到充电器时,您可以获取唤醒锁,因为您打算获取频繁的 GPS 位置,在这种情况下,AlarmManager 似乎不是一个好主意。
当手机未连接到充电器时,您可以使用警报管理器,因为您只会每 5-30 分钟轮询一次位置。获取唤醒锁将显着影响您的电池寿命。
实施取决于你。
干杯....!!
When the phone is connected to the charger u can acquire the wakelock as u a planning to get frequent GPS locations, AlarmManager doesnt seems to be an great idea in this case.
When phone is not connected to the charger u may use alarm manager as u ll only poll for a location once every 5-30 minutes. Acquiring wakelock will affect ur battery life significantly.
Implementation is up to u.
Cheers....!!