[英]Network provider generates a 2 copies of a location
我正在嘗試通過gps提供程序首先生成一個位置,如果我得到一個null,我正在通過網絡提供程序生成。 我的問題是網絡提供商兩次生成相同的位置..我將信息發送到數據庫,並在同一時間發送相同的位置(相同的秒數!)。 這是一些代碼:
gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_gps);
// Update the UI immediately if a location is obtained.
if (gpsLocation != null)
updateUILocation(gpsLocation);
else
updateUILocation(networkLocation);
這是requestUpdateFromProvider:
private Location requestUpdatesFromProvider(final String provider, final int errorResId)
{
Location location = null;
if (mLocationManager.isProviderEnabled(provider))
{
mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS*2, listener);
location = mLocationManager.getLastKnownLocation(provider);
}
else
{
Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
}
return location;
}
這是updateUILocation:
private void updateUILocation(Location location)
{
// We're sending the update to a handler which then updates the UI with the new location.
Message.obtain(mHandler,
UPDATE_LATLNG,
location.getLatitude() + ", " + location.getLongitude()).sendToTarget();
// Bypass reverse-geocoding only if the Geocoder service is available on the device.
if (mGeocoderAvailable)
doReverseGeocoding(location);
}
這是處理程序:
mHandler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == UPDATE_ADDRESS) //UPDATE_ADDRESS = 1
{
LocationService.this.address = (String) msg.obj; // update a string that show the user address
new SendLocation(LocationService.this.id,(String)msg.obj);//send the info to the db.
}
}
};
SendLocation正在執行它假設正確執行的操作,因此您不應該注意它。
編輯
我忘了說第一段代碼是從構造函數調用的方法。
如果創建了重復位置不是您的錯,那么只需調用同步的forwardLocation(Location loc)
方法,該方法存儲最后接收位置的時間戳。 當最后存儲的時間戳等於前一個時間戳時,此方法應忽略該位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.