android功耗(23)---gps定位开发省电要点 android手机gps定位开发省电要点

android手机gps定位开发省电要点

android手机使用GPS定位会非常费电。google工程师在quora上做了具体的解释,有兴趣可以看看

https://www.quora.com/Battery-Life/Why-does-GPS-use-so-much-more-battery-than-any-other-antenna-or-sensor-in-a-smartphone

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 division 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 to 12 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。

来源

https://stackoverflow.com/questions/7872863/keeping-a-gps-service-alive-and-optimizing-battery-life

2、及时注销定位监听

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

[html]  view plain  copy
  1. public void onPause() {  
  2.     super.onPause();  
  3.     locationManager.removeListener(locationListener);  
  4. }  
  5. public void onResume(){  
  6.     super.onResume();  
  7.     locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true),6000,100,locationListener);  
  8. }  

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

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

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

public void requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)

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

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

http://blog.csdn.net/zmpcwl/article/details/10594305


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

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消耗又没有了。



猜你喜欢

转载自blog.csdn.net/zhangbijun1230/article/details/80739327