I want to get Location Updates using this method : FusedLocationProviderClient#requestLocationUpdates(LocationRequest locationRequest, LocationCallback locationCallback, Looper looper)
And most of the answers in Stackoverflow implementing this method had to override LocationCallback methods. One recent example can be found in this question
Code snippet from that question is below :
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
synchronized (SilentCurrentLocationProvider.this) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
}
};
But when I try to initialize LocationCallback like this. I am not forced to override method like the above by Android Studio. And when I inspect the source code of LocationCallback class then I find it as normal java class and onLocationResult method is there but that is not to override.
The only time a compiler forces you to override is in case of an abstract
implementation. The LocationCallBack
class that you see do not contain any abstract member. Notice how they have { }
. This means that it has defined 'concrete' implementation. Another point to note that they are not made final
, which means that they can be overridden or not overridden at all, which explains why the compiler is not forcing you.
You are actually creating an anonymous class and then picking out which one to override. In your case, you picked onLocationResult()
method.
mLocationCallback = new LocationCallback() { //Creating anonymous class to provide our own implementation for onLocationResult
@Override
public void onLocationResult(LocationResult locationResult) {
synchronized (SilentCurrentLocationProvider.this) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
}
};