【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1677|回复: 0

一段Android中处理GPS位置更新太频繁的代码

[复制链接]
发表于 2013-1-29 23:56:23 | 显示全部楼层 |阅读模式
在Android中处理GPS数据的时候,由于经验不丰富,容易出现如下问题,如更新太频繁,返回的Location无数据导致异常,数据不准确等等问题。在实际的项目中,我们也碰到过这些问题,从网上觅得此段代码,并经过细微的修改,应用到我们的GPS项目中,目前没有任何bug。不敢独享,特分享如下。
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.GregorianCalendar;
  5. import java.util.TimeZone;

  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.util.Log;

  12. public class GPSServiceListener implements LocationListener {

  13.     private static final String tag = "GPSServiceListener";
  14.      private long mTimeBetweenLocationEvents;     
  15.     private long mTimeOfLastLocationEvent;     
  16.     private boolean mAccuracyOverride;     
  17.     private float mLastAccuracy;     
  18.     private boolean mOverrideLocation;
  19.     public int GPSCurrentStatus;
  20.      
  21.     private static final float INVALID_ACCURACY = 0.0f;
  22.      
  23.     public GPSServiceListener(long timeBetweenLocationEvents, boolean accuracyOverride)     
  24.     {            
  25.         mTimeOfLastLocationEvent = 0;         
  26.         mAccuracyOverride = accuracyOverride;         
  27.         mLastAccuracy = INVALID_ACCURACY;         
  28.         mOverrideLocation = false;         
  29.         mTimeBetweenLocationEvents = timeBetweenLocationEvents;     
  30.     }      

  31.     //EVENT: onLocationChanged()     
  32.     @Override
  33.      public void onLocationChanged(Location location)     
  34.     {         
  35.         Log.d(tag, "onLocationChanged() triggered. Accuracy = "+Float.toString(location.getAccuracy()));         
  36.         mOverrideLocation = false;         
  37.          if (location != null)         
  38.         {            
  39.             //if a more accurate coordinate is available within a set of events, then use it (if enabled by programmer)            
  40.             if (mAccuracyOverride == true)            
  41.             {                 
  42.                 //whenever the expected time period is reached invalidate the last known accuracy                 
  43.                 // so that we don't just receive better and better accuracy and eventually risk receiving                 
  44.                 // only minimal locations                 
  45.                 if (location.getTime() - mTimeOfLastLocationEvent >= mTimeBetweenLocationEvents)                 
  46.                 {                     
  47.                     mLastAccuracy = INVALID_ACCURACY;                 
  48.                 }                  
  49.                 if (location.hasAccuracy())                 
  50.                 {                     
  51.                     final float fCurrentAccuracy = location.getAccuracy();                     
  52.                
  53.                      if ((fCurrentAccuracy != 0.0f) && (fCurrentAccuracy < mLastAccuracy))                     
  54.                     {                        
  55.                         mOverrideLocation = true;                        
  56.                         mLastAccuracy = fCurrentAccuracy;                     
  57.                     }                 
  58.                 }            
  59.             }                        
  60.              if ((location.getTime() - mTimeOfLastLocationEvent >= mTimeBetweenLocationEvents)||(mOverrideLocation == true) )            
  61.             {                 
  62.                 //be sure to store the time of receiving this event !                 
  63.                 mTimeOfLastLocationEvent = location.getTime();                  
  64.                  //send message to parent containing the location object      
  65.                
  66.                 final StringBuffer strBuffer = new StringBuffer();
  67.                  strBuffer.append(Global.serverUrl);
  68.                  strBuffer.append("position.php?");
  69.                  strBuffer.append("uid=");
  70.                  strBuffer.append(Global.userID);
  71.                  strBuffer.append("&u=");
  72.                  strBuffer.append(Global.userName);
  73.                  strBuffer.append("&p=");
  74.                  strBuffer.append(Global.userPassMD5);
  75.                  strBuffer.append("&la=");
  76.                  strBuffer.append(location.getLatitude());
  77.                  strBuffer.append("&lo=");
  78.                  strBuffer.append(location.getLongitude());
  79.                  strBuffer.append("&s=");
  80.                  strBuffer.append(location.hasSpeed());
  81.                  Log.v(tag, "onLocationChanged 6");
  82.                  //这里启动新的线程提交URL
  83.                  new Thread(new Runnable() {        
  84.                      public void run() {      
  85.                                                  //其他类的静态方法,提交URL      
  86.                          GPSHttpGet.httpGetNoResult(strBuffer.toString());
  87.                      }   
  88.                  }).start();  
  89.              }         
  90.         }     
  91.     }

  92.     @Override
  93.      public void onProviderDisabled(String provider) {
  94.          // TODO Auto-generated method stub
  95.          Log.v(tag, "onProviderDisabled");
  96.      }

  97.     @Override
  98.      public void onProviderEnabled(String provider) {
  99.          // TODO Auto-generated method stub
  100.          Log.v(tag, "onProviderEnabled");
  101.      }

  102.     @Override
  103.      public void onStatusChanged(String provider, int status, Bundle extras)
  104.     {
  105.          Log.v(tag, "onStatusChanged");
  106.          GPSCurrentStatus = status;
  107.      }

  108. }
复制代码
在使用的时候,本代码部分放在一个Service中启动,代码如下:
  1. public void startService()
  2.      {
  3.          locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  4.          locationListener = new GPSServiceListener(CONST.LOCATION_UPDATE_PERIOD_MSEC,true);
  5.          Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  6.          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
  7.      }

  8.     public void endService()
  9.      {
  10.          if(locationManager != null && locationListener != null)
  11.          {
  12.              locationManager.removeUpdates(locationListener);
  13.          }

  14.     }
复制代码
参考代码:
LocationManager calling onLocationChanged too often?

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-5-6 14:09

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表