我正在尝试编写简单的方法来通过 Google Play 服务获取位置。像这样的东西(是的,它可能还有其他一些问题..):
Location getLastLocation() {
final Location location = new Location("");
locationClient.registerConnectionCallbacks(new GooglePlayServicesClient.ConnectionCallbacks() {
@Override
public void onConnected(final Bundle connectionHint) {
location.set(locationClient.getLastLocation());
locationClient.unregisterConnectionCallbacks(this);
locationClient.disconnect();
}
@Override
public void onDisconnected() {};
});
if (!locationClient.isConnecting() || !locationClient.isConnected()) {
locationClient.connect();
}
return location;
}
显然,它将返回 empty Location
。那么,我怎样才能让它等待回调完成呢?当然,在不阻塞主线程的情况下......