Context:
I have been using FusedLocationProviderClient
to get user's location.
When the phone is in High Accuracy mode, everything works as expected.
However, not all phones are in high accuracy.
So, how can I manage to get the user location by GPS only
What I've tried so far:
1 - Tried this with FusedLocationProviderClient
:
providerClient = LocationServices.getFusedLocationProviderClient(this);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Log.e(TAG,"CALLBACK RECEIVED");
processNewLocation(locationResult.getLastLocation());
}
};
locationRequest = new LocationRequest();
locationRequest.setInterval(LocationConstant.UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(LocationConstant.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
providerClient.requestLocationUpdates(locationRequest,
locationCallback, Looper.myLooper());
When location is in Device Only, location callback is never called, but works perfectly if it is in high accuracy(Also changed setPriority()
to every priority and no luck).
2-Tried this with Location Manager:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener());
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, new MyLocationListener());
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
Still the same result