androidlocationfusedlocationproviderapiandroid-fusedlocationfusedlocationproviderclient

Android battery consumption in case of frequent call of fusedLocationProviderClient lastKnownLocation?


I have this use-case of sending the user's location when I hit a certain API. As this API call is frequent, I want to avoid the battery consumption of the app.

I am okay with using the lastKnownLocation of the user and sending the same.

fun getLastLocation(): Single<LocationUpdate> {
    return Single.create { emitter ->
        fusedLocationproviderClient.lastLocation.addOnSuccessListener { location ->
            if (location != null) {
                emitter.onSuccess(LocationUpdate.Valid(location.lat, location.long))
            } else {
                emitter.onSuccess(LocationUpdate.UnKnown)
            }
        }
        .addOnFailureListener {
                emitter.tryOnError(it)
       }
    }
}

This function getLastLocation can get called many times, and I read that fusedLocationproviderClient.lastLocation is battery efficient and doesn't make a new API call every time and relies on other apps to get the location.

Wanted to understand and know, if it's okay to use this getLastLocation call frequently in the app and it's battery efficient or I should think of another way like cache it and set the expiry time to it.


Solution

  • lastLocation is just a cached location like you said so yes it is ok to use as long as you understand what it is which it sounds like you do