Estoy desarrollando una aplicación Android donde quiero obtener la ubicación lo más rápido posible (cada segundo estaría bien).
Por ahora he añadido los servicios de google play de localización como he visto en muchos tutoriales y consigo obtener la localización correcta del dispositivo.
El problema es que en el código:
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(500);
parece que no hace nada porque obtengo los updates de la localización cada 10-15 segundos.
Añado todo el código:
public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient fusedLocationClient;
public final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
public final long UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
final int LOCATION_REQUEST = 1;
LocationRequest locationRequest;
LocationCallback locationCallback;
TextView velocidad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
velocidad = findViewById(R.id.velocidad);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setSmallestDisplacement(0f);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
settingsClient.checkLocationSettings(locationSettingsRequest);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
onLocationChanged(locationResult.getLastLocation());
}
};
startLocationUpdates();
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQUEST);
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
public void onLocationChanged(Location location) {
// New location has now been determined
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude()) + " Velocidad: "+location.hasSpeed();
System.out.println(msg);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
startLocationUpdates();
}
}
El problema es el tiempo de actualización que me da las ubicaciones cada 10-15 segundos en vez de cada segundo y no consigo que el tiempo baje, gracias de antemano.