Keeping a GPS service alive and optimizing battery life(保持 GPS 服务有效并优化电池寿命)
问题描述
我必须构建一个运行大约一天的 GPS 跟踪器的应用程序.我知道 SO 中有类似的问题,但我还没有找到一些问题的答案.
I must build an application with a GPS tracker running during about a day. I'm aware of similar questions in SO but I haven't found any answers to some questions I have.
-我需要每 10 分钟进行一次 GPS 修复,所以我认为最好的方法是启动定位服务,获得修复(或超时)并停止服务(使用 removeUpdates()代码>).我怎样才能让应用程序(或服务或其他)每 10 分钟运行一次此周期,并确保只要有剩余电池电量它就会继续运行(即使设备进入睡眠状态,它也应该每 10 分钟唤醒一次以获得修复)?使用 AlarmManager 是个好主意吗?
-I need a GPS fix every 10 min, so I think the best way to do it is to start the location service, get a fix (or timeout) and stop the service (with removeUpdates()
). How can I have an application (or service or whatever) running this cycle every 10min and be sure it will continue as long as there is some battery left (even if device goes to sleep, it should wake it up every 10min to get a fix)? Is using AlarmManager a good idea?
-我可以期望电池使用这种方法可以使用一天吗?
-Can I expect the battery to last one day with this method?
我已经检查了 mytracks,但 gps 监听器似乎始终处于开启状态,并且预计电池电量持续不超过 5 小时.
I've checked mytracks but the gps listener seems always on and the battery is expected to last no more than 5h.
我还检查了 CWAC 位置轮询器,但它只检查了 removeUpdates()
超时并立即重新启动侦听器.它还使用唤醒锁,而在我的情况下,我认为 AlarmManager 可能是一个更好的主意.
I've also checked CWAC Location Poller but it does only removeUpdates()
on timeout and restart the listener immediately. It also uses a wakelock while in my case I think an AlarmManager could be a better idea.
欢迎任何帮助/建议
推荐答案
你对我使用的警报管理器很满意
You are spot on with alarm manager I use
Intent serviceIntent = new Intent(this, TrackerService.class);
mPendingIntent = PendingIntent.getService(this, 0, serviceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(mPendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), interval, mPendingIntent);
在类似的应用中获取网络位置
in a similar app for getting network location
启动服务的间隔是毫秒
服务启动,获取位置并关闭
the service starts up, gets the location and closes
这比等待报告的活动服务更省电
this was MUCH more battery efficient that hanging around with an active service waiting for reports
我发布的代码首先取消了以前的警报,所以你不会得到超过 1 个 :)
that code i posted cancels the previous alarms first so you don't get more than 1 :)
这篇关于保持 GPS 服务有效并优化电池寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:保持 GPS 服务有效并优化电池寿命
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01