Mauro Marchiori
Mauro Marchiori

Reputation: 21

location service slow update

I implemented into my app a location service that uses FusedLocationProviderClient in order to track my position every 1 second when tracking is started. The tracking works correctly, but after about 10-15 minutes of active tracking, the update becomes slower, it updates the position every 3 seconds, and after 20-30 minutes..about 5 seconds. The app, after this time, becomes jerky. The position, altitude, bearing and time are recorded every second into a database (Room database) that remains opened until the stop of tracking. Do someone had a similar issue? Can it be connected to the phone memory due to many data saved or to a sort of "saving mode" made independently by FusedLocationProviderClient? I am trying that on a Samsung S9+ that should not have memory issues. Is there a lighter way? I post here below the code for the location service:

public class LocationService extends Service {
    FusedLocationProviderClient fusedLocationProviderClient;
    LocationCallback locationCallback;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                Intent intent = new Intent("ACT_LOC");
                intent.putExtra("latitude", locationResult.getLastLocation().getLatitude());
                intent.putExtra("longitude", locationResult.getLastLocation().getLongitude());
                intent.putExtra("altitude", locationResult.getLastLocation().getAltitude());
                intent.putExtra("precision", locationResult.getLastLocation().getAccuracy());
                intent.putExtra("speed", locationResult.getLastLocation().getSpeed());
                intent.putExtra("time", locationResult.getLastLocation().getTime());
                intent.putExtra("bearing",locationResult.getLastLocation().getBearing());
                sendBroadcast(intent);
            }
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        requestLocation();
        return super.onStartCommand(intent, flags, startId);
    }

    private void requestLocation() {
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(1000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
    }
}

Upvotes: 1

Views: 675

Answers (1)

fdermishin
fdermishin

Reputation: 3706

It looks like you have some sort of leaks. Like creating a new subscription to location updates every time you get a location. You can check for memory leaks or add logging to verify that the all code that handles location updates runs exactly once for each new location.

Upvotes: 1

Related Questions