真的没那么难。代码的作用是接收找到的位置的持续更新;您可以让多个听众收听不同的提供商,因此这些更新可能或多或少取决于提供商(例如 GPS 可能比网络更准确)。isBetterLocation(...)
评估侦听器找到的位置是否实际上比您已经知道的位置更好(并且应该在您的代码中引用)。isBetterLocation(...) 代码有据可查,所以应该不难理解,但第一个参数location是提供者找到的新位置,currentBestLocation是您已经知道的位置。
我使用的代码与您的代码大致相同,只是我不只是采用最佳提供商。处理程序的内容是因为我不想继续更新,只需在两分钟的最大时间范围内找到对我来说足够准确的最佳位置(GPS 可能需要一点时间)。
private Location currentBestLocation = null;
private ServiceLocationListener gpsLocationListener;
private ServiceLocationListener networkLocationListener;
private ServiceLocationListener passiveLocationListener;
private LocationManager locationManager;
private Handler handler = new Handler();
public void fetchLocation() {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
LocationProvider passiveProvider = locationManager.getProvider(LocationManager.PASSIVE_PROVIDER);
//Figure out if we have a location somewhere that we can use as a current best location
if( gpsProvider != null ) {
Location lastKnownGPSLocation = locationManager.getLastKnownLocation(gpsProvider.getName());
if( isBetterLocation(lastKnownGPSLocation, currentBestLocation) )
currentBestLocation = lastKnownGPSLocation;
}
if( networkProvider != null ) {
Location lastKnownNetworkLocation = locationManager.getLastKnownLocation(networkProvider.getName());
if( isBetterLocation(lastKnownNetworkLocation, currentBestLocation) )
currentBestLocation = lastKnownNetworkLocation;
}
if( passiveProvider != null) {
Location lastKnownPassiveLocation = locationManager.getLastKnownLocation(passiveProvider.getName());
if( isBetterLocation(lastKnownPassiveLocation, currentBestLocation)) {
currentBestLocation = lastKnownPassiveLocation;
}
}
gpsLocationListener = new ServiceLocationListener();
networkLocationListener = new ServiceLocationListener();
passiveLocationListener = new ServiceLocationListener();
if(gpsProvider != null) {
locationManager.requestLocationUpdates(gpsProvider.getName(), 0l, 0.0f, gpsLocationListener);
}
if(networkProvider != null) {
locationManager.requestLocationUpdates(networkProvider.getName(), 0l, 0.0f, networkLocationListener);
}
if(passiveProvider != null) {
locationManager.requestLocationUpdates(passiveProvider.getName(), 0l, 0.0f, passiveLocationListener);
}
if(gpsProvider != null || networkProvider != null || passiveProvider != null) {
handler.postDelayed(timerRunnable, 2 * 60 * 1000);
} else {
handler.post(timerRunnable);
}
} catch (SecurityException se) {
finish();
}
}
private class ServiceLocationListener implements android.location.LocationListener {
@Override
public void onLocationChanged(Location newLocation) {
synchronized ( this ) {
if(isBetterLocation(newLocation, currentBestLocation)) {
currentBestLocation = newLocation;
if(currentBestLocation.hasAccuracy() && currentBestLocation.getAccuracy() <= 100) {
finish();
}
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String s) {}
@Override
public void onProviderDisabled(String s) {}
}
private synchronized void finish() {
handler.removeCallbacks(timerRunnable);
handler.post(timerRunnable);
}
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
//etc
}
private Runnable timerRunnable = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(LocationService.this.getPackageName() + ".action.LOCATION_FOUND");
if(currentBestLocation != null) {
intent.putExtra(LocationManager.KEY_LOCATION_CHANGED, currentBestLocation);
locationManager.removeUpdates(gpsLocationListener);
locationManager.removeUpdates(networkLocationListener);
locationManager.removeUpdates(passiveLocationListener);
}
}
};