2

I'm testing the accuracy of the FusedLocationProviderClient's location updates with different priorities. In the application I am first getting 15 updates with PRIORITY_HIGH_ACCURACY, then canceling the request, changing the priority to PRIORTIY_LOW_POWER and requesting again. I have also tried changing to ACCURACY_BALANCED_POWER_ACCURACY and in both cases PRIORITY_HIGH_ACCURACY has the worst performance in terms of accuracy - every single time I run my app.

Another thing I've noticed is that in the first 15 updates, the accuracy is changing quite often, while the last 15 pretty much give the same accuracy all the way. Any idea why this is happening? Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtBatteryLevel = (TextView) findViewById(R.id.batteryLevel);
    txtAccuracy = (TextView) findViewById(R.id.accuracy);

    mFusedLocationClient = 
    LocationServices.getFusedLocationProviderClient(this);

    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(0);

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult){
            onLocationChanged(locationResult.getLastLocation());
        }
    };


    requestLocationUpdates();
}

public void requestLocationUpdates() {
    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}

public void onLocationChanged(Location location){
    int batteryLevel = Integer.valueOf(txtBatteryLevel.getText().toString());
    if(batteryLevel>0) {
        if (mLocationRequest.getPriority() == LocationRequest.PRIORITY_HIGH_ACCURACY) {
            if (batteryLevel <= 15) { 
           mFusedLocationClient.removeLocationUpdates(mLocationCallback);
           mLocationRequest = new LocationRequest();
           mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
           mLocationRequest.setInterval(0);

           requestLocationUpdates();
           return;
            }
        }
        txtAccuracy.setText(String.valueOf(location.getAccuracy()));

        batteryLevel--;
        txtBatteryLevel.setText(String.valueOf(batteryLevel));
    }
}
Jesper
  • 2,644
  • 4
  • 30
  • 65

1 Answers1

-1

Check if below code helps you...

locationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);
                    for (Location location : locationResult.getLocations()) {
                        if (location != null) {
                            mLastKnownLocation = location;
                        }
                    }
                }
            };
Nishchay
  • 11
  • 2
  • 1
    Can you add some explanation on why the code solves the issue, and possibly add comments to the code explaining it further? You may also want to take a look at [this StackOverflow article](https://stackoverflow.com/help/how-to-ask). – Marcello B. Oct 09 '18 at 23:45