使用LocationClient(v2 API)以同步方式获取Android上的上次已知位置的“正确”方法是什么?
更新 这是我想到的最好方法(它不是同步的,但克服了每次需要上次已知位置时处理connect()和onConnected()的负担):
更新 这是我想到的最好方法(它不是同步的,但克服了每次需要上次已知位置时处理connect()和onConnected()的负担):
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(); // fire it already so subsequent calls get the real location
}
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();
}
}
我的Java技能……还不够发展……有什么改进建议吗?