保持 GPS 服务有效并优化电池寿命 [英] Keeping a GPS service alive and optimizing battery life

查看:15
本文介绍了保持 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 服务有效并优化电池寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆