Reputation: 351
I am making a location request using FusedLocationProviderClient. I have kept a timer for 30 seconds to check if location is received within that timeframe. Code is below:
public void requestLocationUpdates() {
initializeLocationRequest();
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
startTimer();
}
private void initializeLocationRequest() {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext);
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
synchronized (SilentCurrentLocationProvider.this) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
}
};
}
private void startTimer() {
if (mTimeout < 1) {
mTimeout = mDefaultTimeout;
}
mTimer = new Timer();
mTimer.schedule(new LocationTimer(), mTimeout * 1000);
}
On few devices, location is not coming. On debugging further, I found that LocationRequest and LocationCallback are being created, but it's not coming inside onLocationResult() method.
Since this is part of a live product, it's hard to debug on every device. Even I tried restarting the phone but it doesn't work. The user tried on other applications like Ola and location was coming there.
Location Permissions have been given as well.
Can anyone tell me possible reasons for this issue?
Upvotes: 29
Views: 11033
Reputation: 612
You need to request only the following permission:
Manifest.permission.ACCESS_FINE_LOCATION
I had the same issue and I was requesting both permissions with:
Manifest.permission.ACCESS_COARSE_LOCATION
When I removed COARSE LOCATION I got the update almost immediately.
Upvotes: 6
Reputation: 3600
This happened to me when the location was turned off. Not the location permission for the app, but the location for the android device itself. Requesting that the user activate the GPS fixed it for me:
protected void onCreate(Bundle savedInstanceState) {
//...
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Solution from here.
You may also need to turn the GPS off and then on again to trigger a dialog to improve location accuracy:
Upvotes: 3
Reputation: 341
Had the same issue on mobiles with Android Pie. Location permission was granted(manually by going to app settings) and location was on with high accuracy but wasn't getting location updates. Got fixed after i added code to check if location permission was granted before requesting for location updates.
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted. Request for permission
}
Upvotes: 3
Reputation: 69
I was facing this same issue. In my case the device-location method/accuracy was set to Phone only rather than High accuracy or Battery saving. Change the settings to High accuracy and receive callbacks on locationCallback
.
Upvotes: 5