| | |
安卓開(kāi)發(fā)中,在處理GPS數據的時(shí)候,容易出現如下問(wèn)題,如GPS位置更新太頻繁,返回的Location無(wú)數據導致異常,數據不準確等等問(wèn)題。許多安卓開(kāi)發(fā)者在處理GPS位置信息時(shí)候也可能會(huì )遇到這樣的問(wèn)題,下面提供一種方法,這里是一段代碼,本文代碼來(lái)自網(wǎng)上整理,下面請看參考代碼:
public class GPSServiceListener implements LocationListener {
private static final String tag = "GPSServiceListener";
private long myTimeBetweenLocationEvents;
private long myTimeOfLastLocationEvent;
private boolean myAccuracyOverride;
private float myLastAccuracy;
private boolean myOverrideLocation;
public int GPSCurrentStatus;
private static final float INVALID_ACCURACY = 0.0f;
public GPSServiceListener(long timeBetweenLocationEvents, boolean accuracyOverride)
{
myTimeOfLastLocationEvent = 0;
myAccuracyOverride = accuracyOverride;
myLastAccuracy = INVALID_ACCURACY;
myOverrideLocation = false;
myTimeBetweenLocationEvents = timeBetweenLocationEvents;
}
//EVENT: onLocationChanged()
@Override
public void onLocationChanged(Location location)
{
Log.d(tag, "onLocationChanged() triggered. Accuracy = "+Float.toString(location.getAccuracy()));
myOverrideLocation = false;
if (location != null)
{
//if a more accurate coordinate is available within a set of events, then use it (if enabled by programmer)
//在一組事件中如果一個(gè)更精確的坐標可用,然后使用它(如果通過(guò)程序)
if (myAccuracyOverride == true)
{
//whenever the expected time period is reached invalidate the last known accuracy
// so that we don't just receive better and better accuracy and eventually risk receiving
// only minimal locations
if (location.getTime() - myTimeOfLastLocationEvent >= myTimeBetweenLocationEvents)
{
myLastAccuracy = INVALID_ACCURACY;
}
if (location.hasAccuracy())
{
final float fCurrentAccuracy = location.getAccuracy();
if ((fCurrentAccuracy != 0.0f) && (fCurrentAccuracy < myLastAccuracy))
{
myOverrideLocation = true;
myLastAccuracy = fCurrentAccuracy;
}
}
}
if ((location.getTime() - myTimeOfLastLocationEvent >= myTimeBetweenLocationEvents)||(myOverrideLocation == true) )
{
//be sure to store the time of receiving this event 要確保保存接收這個(gè)事件的時(shí)間
myTimeOfLastLocationEvent = location.getTime();
//send message to parent containing the location object 發(fā)送消息到父包含location對象
final StringBuffer strBuffer = new StringBuffer();
strBuffer.append(Global.serverUrl);
strBuffer.append("position.php?");
strBuffer.append("uid=");
strBuffer.append(Global.userID);
strBuffer.append("&u=");
strBuffer.append(Global.userName);
strBuffer.append("&p=");
strBuffer.append(Global.userPassMD5);
strBuffer.append("&la=");
strBuffer.append(location.getLatitude());
strBuffer.append("&lo=");
strBuffer.append(location.getLongitude());
strBuffer.append("&s=");
strBuffer.append(location.hasSpeed());
Log.v(tag, "onLocationChanged 6");
//這里啟動(dòng)新的線(xiàn)程提交URL
new Thread(new Runnable() {
public void run() {
//提交URL,其他類(lèi)的靜態(tài)方法
GPSHttpGet.httpGetNoResult(strBuffer.toString());
}
}).start();
}
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.v(tag, "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.v(tag, "onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.v(tag, "onStatusChanged");
GPSCurrentStatus = status;
}
}
在使用的時(shí)候,本代碼部分放在一個(gè)Service中啟動(dòng),代碼如下:
public void startService()
{
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSServiceListener(CONST.LOCATION_UPDATE_PERIOD_MSEC,true);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
}
public void endService()
{
if(locationManager != null && locationListener != null)
{
locationManager.removeUpdates(locationListener);
}
}
本文整理自網(wǎng)絡(luò ),希望對讀者有所幫助。
聯(lián)系客服