【发布时间】:2016-04-20 10:18:32
【问题描述】:
我正在使用 GoogleApiClient 获取用户位置。我创建了一个单例类来获取用户位置。这是那个类:
public class LocationUtils {
private static LocationUtils locationUtils;
private GoogleApiClient googleApiClient;
public static LocationUtils getInstance() {
if(locationUtils == null)
locationUtils = new LocationUtils();
return locationUtils;
}
private LocationUtils() {}
public Location getLocation(Context context){
final Location[] location = {null};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient );
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi( LocationServices.API )
.build();
googleApiClient.connect();
return location[0];
}
}
我想通过 LocationUtils.getInstance().getLocation(context) 获取用户位置。当我调用 getLocation() 方法时,它应该连接到 GoogleApiClient,返回用户位置并在每次调用时断开与 GoogleApiClient 的连接。但是当我调用该方法时,它返回null。主线程不等待 onConnected() 方法并将位置对象返回为 null。它如何等待 onConnect() 方法?或者如何在 onConnected() 中返回 Location 对象?
已解决根据 Tim Castelijns 的回答
首先我创建了一个接口:
public interface LocationCallbackInterface {
void onComplete(Location location);
}
然后在 getLocationMethod() 中使用它:
public void getLocation(上下文上下文,最终 LocationCallbackInterface 回调){
final Location[] location = {null};
googleApiClient = new GoogleApiClient.Builder(App.getContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
//Toast.makeText(App.getContext(), "location came :" + location[0].toString(), Toast.LENGTH_LONG).show();
callback.onComplete(location[0]);
}
googleApiClient.disconnect();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi( LocationServices.API )
.build();
googleApiClient.connect();
}
然后在活动中调用该方法:
LocationUtils.getInstance().getLocation(getActivity(), new LocationCallbackInterface() {
@Override
public void onComplete(Location location) {
if (location != null) {
Toast.makeText(getActivity(), "location :" + location.toString(), Toast.LENGTH_SHORT).show();
initCamera(location);
} else
Toast.makeText(getActivity(), "location is null", Toast.LENGTH_SHORT).show();
}
});
【问题讨论】:
标签: android multithreading google-maps android-location google-api-client