设置标准只是根据它们确定最好使用哪个提供商,因此对位置的准确性或有效性没有真正的发言权。我只是立即将提供程序设置为 GPS(如果 GPS 可用!)。
此外,您似乎没有根据时间和距离对您希望等待多长时间进行更新提出任何要求。这是我使用意图和广播接收器所做的示例。它可能会帮助你。
public void beginMonitoringLocation(int minDistance) {
IntentFilter filter = new IntentFilter();
filter.addAction(MainActivity.LOCATION_UPDATE_ACTION);
this.mContext.registerReceiver(this.locationReceiver, filter);
LocationManager mLocationManager = (LocationManager) this.mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.addGpsStatusListener(this);
boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Log.e("LocationManager", "GPS not enabled!!!!");
}
LocationProvider provider = mLocationManager.getProvider(LocationManager.GPS_PROVIDER); // GET THE BEST PROVIDER FOR OUR LOCATION
Log.d("LocationManager:","Location Provider:"+provider);
if ( provider == null ) {
Log.e( "LocationManager", "No location provider found!" );
return;
}
final int locationUpdateRC=0;
int flags = 0;
Intent intent = new Intent(MainActivity.LOCATION_UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.mContext, locationUpdateRC, intent, flags);
// PENDING INTENT TO BE FIRED WHEN THE LOCATIONMANAGER RECEIVES LOCATION UPDATE.
// THIS PENDING INTENT IS CAUGHT BY OUR BROADCAST RECEIVER
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,minDistance,pendingIntent);
this._monitoringLocation = true;
}
然后在同一个班级我把我的broadcast receiver
public BroadcastReceiver locationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Location location = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
if (location != null) {
//Do something with it
}
}
};
我的意图过滤器的操作只是对我的活动中的一个常量集的静态引用。
public static final String LOCATION_UPDATE_ACTION = "com.corecoders.sqlmaptrack.LOCATION_UPDATE_RECEIVED";
这在我的情况下为我提供了准确的位置。如果需要,您可以将距离设置为 0,然后您会发现,如果您有 4 颗或更多卫星的良好定位,您将获得每秒 5 次精度的定位。
我希望这可以帮助你