Android应用开发之gps定位开发省电要点
白羽 2018-12-19 来源 :网络 阅读 376 评论 0

摘要:本文将带你了解Android应用开发android功耗之gps定位开发省电要点,希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发android功耗之gps定位开发省电要点,希望本文对大家学Android有所帮助。


  

android功耗之gps定位开发省电要点

android手机使用GPS定位会非常费电

GPS is expensive because it is a very slow communication channel—you need to communicate with three or four satellites for an extended duration at50 bits per second. There is no time pision as in other communication mechanisms, necessitating powering the antenna for the duration of any communication. Worse, while the GPS is on, the system cannot enter a sleep state. Mobile devices such as Android and the iPhone achieve their battery life largely because they can aggressively and quickly enter into and exit from sleep states.GPS prevents this.

GPS's battery draining behavior is most noticeable during the initial acquisition of the satellite's navigation message: the satellite's state, ephemeris, and almanac. Acquiring each satellite takes 12 to 30 seconds, but if the full almanac is needed, this can take up to12 minutes. During all of this, your phone is unable to enter a deep sleep. A-GPS (Assisted GPS) partially solves this, by sending the navigational message to your mobile device over your cellular data network or even Wi-Fi. As the bandwidth of either of these greatly dwarves the 50bps of the GPS satellites, the time spent powering the GPS antenna or avoiding deep sleep is greatly reduced.

Nonetheless, even with A-GPS,using your GPS is a noticeable battery hog. This is again due not to powering the GPS itself, but by preventing the phone from going to sleep. Compounding the cost, most mapping software is processor-intense. A well-designed app can make a significant difference here; Google Maps boasts several optimizations to reduce battery consumption from GPS usage.

1、解决方法就是这个帖子里面讲的,每隔一段时间才监听一次GPS信息。当监听到GPS信息后,关闭GSP,设置一个定时器,等待若干时间后重启GPS。

2、及时注销定位监听

在获取到定位之后或者程序处于后台时,注销定位监听,此时监听GPS传感器相当于执行no-op(无操作指令),用户不会有感知但是却耗电。

[html]view plaincopy

publicvoidonPause(){

super.onPause();

locationManager.removeListener(locationListener);

}

publicvoidonResume(){

super.onResume();

locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria,true),6000,100,locationListener);

}

来源https://www.jianshu.com/p/5d83d8649c98

3、设置较长的GPS更新的最短时间,设置最少间隔1秒钟

Android程序调用GPS时,都是直接用函数

public voidrequestLocationUpdates(long minTime, float minDistance,Criteriacriteria,PendingIntentintent)

其中,minTime是间隔多长时间获得一次GPS更新,minDistance是间隔多少距离获得一次GPS更新。

注1:两者都设置时(都不为0),那必须在两个条件同时满足时才通知我们的应用。注2:由于长期使用GPS模块非常耗电,所有Android建议我们在允许的范围内尽可能地将minTime设置长一些(特别是后台的线程)注3:当minTime设置不为0(哪怕是1秒),每隔一秒尝试去获取gps数据时都要重新搜星(也就是不会基于上次的定位快速优化并提高精度)。

百度地图或者其他地图也是类似的。

4、用Battery Historian分析电池消耗。

为了搞清楚持续监听和间隔监听两种方法的耗电情况,做了一次对比

第一张图是持续监听,调用requestLocationUpdates之后,一直在监听,不停止。可以看到08:58:21到09:10:48这段12分钟时间gps一直在耗电。

 

第二张图是间隔监听,调用requestLocationUpdates之后,等到获得第一个正确的gps位置之后就调用removeGpsStatusListener停止监听,然后在5分钟之后重新监听。可以看到20:58:21到20:59:00这段时间gps在耗电,然后在后续的5分钟没有gps消耗,在21::04:05开始重新监听,开始了gps消耗,15秒后获得了新的gps地址,又停止监听,gps消耗又没有了。

 

          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程