What would be the “right” way to get the last known location on Android using LocationClient (v2 API) using synchronous ?
UPDATE
This is the best I came up with (it is not synchronous, but it overcomes the burden of working with connect()and onConnected()every time the last known location is required):
public enum SystemServicesNew implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
INSTANCE;
private LocationClient mLocationClient;
private Location mLastKnownLocation;
static {
INSTANCE.mLocationClient = new LocationClient(MyApp.getAppContext(), INSTANCE, INSTANCE);
INSTANCE.mLastKnownLocation = new Location("");
INSTANCE.mLastKnownLocation.setLatitude(0);
INSTANCE.mLastKnownLocation.setLongitude(0);
INSTANCE.getLastKnownLocation();
}
public Location getLastKnownLocation()
{
if(!mLocationClient.isConnected()) {
mLocationClient.connect();
return mLastKnownLocation;
}
mLastKnownLocation = mLocationClient.getLastLocation();
return mLastKnownLocation;
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(MyApp.getAppContext(), "LocationClient:Connected", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected() {
Toast.makeText(MyApp.getAppContext(), "LocationClient:Disconnected", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(MyApp.getAppContext(), connectionResult.toString(), Toast.LENGTH_SHORT).show();
}
}
My Java skills ... are less developed ... any suggestions for improvement?
source
share