如何在GoogleApiClient onConnected()上返回值?

时间:2016-04-20 10:18:32

标签: android multithreading google-maps android-location google-api-client

我正在使用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()方法并将location对象返回为null。它如何等待onConnect()方法?或者如何在onConnected()中返回Location对象?

根据Tim Castelijns的回答

已解决

首先我创建了一个界面:

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

然后在getLocationMethod()中使用它:

public void getLocation(Context context,final LocationCallbackInterface callback){

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();
            }
        });

1 个答案:

答案 0 :(得分:2)

由于该位置是异步获取的,因此您应该使用回调来返回该位置,否则您的返回值将始终为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.
    }
});