如何在 GoogleApiClient onConnected() 上 return 值?

How to return value on GoogleApiClient onConnected()?

我正在使用 GoogleApiClient 获取用户位置。我创建了一个单例 class 来获取用户位置。这是 class:

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,return 用户位置,并在每次调用时断开与 GoogleApiClient 的连接。但是当我调用该方法时,它 return 为空。主线程不等待 onConnected() 方法和 returns 位置对象为空。它如何等待 onConnect() 方法?或者我如何 return 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();

}

然后在 activity:

中调用该方法
 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();
            }
        });

由于位置是异步获取的,您应该使用回调到 return 位置,否则您的 return 值将始终为 null,因为 googleApiClient 尚未完成。

像这样定义一个接口

public interface LocationCallback {
    void onComplete(Location location);
}

改变

public Location getLocation(Context context)

public Location getLocation(Context context, LocationCallback callback)

然后这样称呼它

LocationUtils.getLocation(context, new LocationCallback() {
    @Override
    onComplete(Location location) {
        // when this is called, you will have a location.
    }
});