我开始使用 FusedLocationClient,但我不确定为什么我需要 OnSuccessListener 和 LocationCallback。不应该只是其中之一就足够了吗?
private void initLocationCallback(Context context) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
onLocationChanged(location);
}
});
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
for (Location location : locationResult.getLocations()) {
if (location != null) {
onLocationChanged(location);
}
}
}
}
};
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, context.getMainLooper());
}
private void onLocationChanged(Location location) {
// use location...
}