Problem:
I have tried using the new FusedLocationProviderClient
and the deprecated FusedLocationApi
to receive location updates on my app using a Sony Xperia Z2 and Huawei Honor 8. Everything works fine initialy. However it seems that after x amount of location requests the accuracy number NEVER drops below 10.0 using location.getAccuracy(); // 10.0
What I have tried:
Uninstalling and reinstalling the app does no difference. Restarting the phone, same result. Yes I have LocationRequest.PRIORITY_HIGH_ACCURACY
... no different result.
What might have worked:
Did a factory reset. Whooppaa suddenly I am now getting numbers below 10.0 as expected with clear conditions outside. But why??
Question:
I suspect the system is limiting my accuracy after x amount of location updates (to save battery???)... What do you guys think? What could possibly be the issue here?
EDIT:
After doing a software reset on the phone i was getting accuracy numbers below 10 again initialy. But again after x amount of location updates the accuracy number is limited to 10.0m. I should say that my GPS is designed to run as foreground for up to maybe 3.5 hours at a time. And yes i know it is battery draining.
public class GPSServiceNew extends Service{
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private long UPDATE_INTERVAL = 10 * 1000; /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String test = "";
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
mFusedLocationClient = getFusedLocationProviderClient(this);
startLocationUpdates();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("My Awesome App")
.setContentText("Doing some work...")
.setContentIntent(pendingIntent).build();
startForeground(100, notification);
}
// Trigger new location updates at interval
@SuppressLint("MissingPermission")
protected void startLocationUpdates() {
// Create the location request to start receiving updates
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
settingsClient.checkLocationSettings(locationSettingsRequest);
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
// do work here
onLocationChanged(locationResult.getLastLocation());
}
},
Looper.myLooper());
}
public void onLocationChanged(Location location) {
// New location has now been determined
String msg = "Updated Location: " +
String.valueOf(location.getAccuracy());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
// You can now create a LatLng Object for use with maps
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Log.d("GPS: ", String.valueOf(location.getAccuracy()));
}
}