I'm having delay problems in getting the GPS coordinates. I get them the moment the user presses a textbox to validate the data. Motorola devices go well. Now testing on the OnePlus 3T (A3000) and on LG the delay has between 12 and 15 seconds, no matter the provider or wherever you are (garage, subsoil, in a building, etc). And no matter how many times the user responds to the same field, the problem happens.
I read these links in the OS:
Getting too much delay from OnLocationChange listener.?
Why Android LocationManager has long delay before location updates start if setting
My code:
public void getLocation(final LocationListener locationListener) {
if (locationListener == null) return;
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(500);
locationRequest.setFastestInterval(500);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
.setAlwaysShow(true);
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
LocationManager locationManager =
((LocationManager) getSystemService(Context.LOCATION_SERVICE));
showError("NETWORK: "+ locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
showError("GPS: "+locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
String provider = locationManager.getBestProvider(criteria, true);
if (App.isVersionGreaterThanLollipop()){
if (ActivityCompat.checkSelfPermission(
ChecklistActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Location loc = locationManager.getLastKnownLocation(provider);
currentLcoation = loc;
showError("BEST PROVIDER: "+provider+" CURRENT LOCATION: " +loc.getLatitude()+" "+loc.getLongitude());
locationManager.requestSingleUpdate(
criteria, locationListener, null);
}
else {
requestLocationPermission(locationListener);
}
...
}
...
Not even forcing NETWORK_PROVIDER I get a location quickly.
For those who have faced this type of problem, what is the best approach to get the coordinates in this scenario quickly? Note that I'm using requestSingleUpdate. Does this influence the fixes?