An online markdown blog and knowledge repository.
Acquried via the Google Play Services "Location API".
Last Known Location is assumed to be equivalent to Current Location.
Fused Location Provider: Actual component that retrieves location data. Manages underlying location technology. Provdies simple API to specify high accuracy while using minimal power.
Utilize getLastLocation()
to acquire location information from Fused Location Provider.
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
getLastLocation()
method which returns a Task.getLastLocation()
which minimizes battery usage, or getCurrentLocation()
which causes active processing of location determination, utilizing more battery power.Avoid using requestLocationUpdates()
as it is a power-hungry process and could linger in memory if not closed properly, further draining battery power.
As they existed at developer.android.com when accessed...
Adding to the onCreate() method:
private FusedLocationProviderClient fusedLocationClient;
// ..
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
Acquiring Last Known Location:
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
When building location-aware Apps:
Get Last Known Location from Developer Android Docs
Return to Root Readme