等待 onConnected 调用,获取位置

Wait for onConnected call, to get location

所以,我有自定义定位服务 class,我想从中获取最后已知的位置。我有可能在连接 GoogleApiClient 之前调用 getLastKnownLocation(),所以我必须等待它然后调用 getLastKnownLocation(),但我不知道如何管理它。我认为 RxJava 2 可以帮助我,但我还不熟悉那个框架。这是我现在的 class:

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;

import javax.inject.Inject;

import pl.pancor.android.air.base.FragmentScope;

@FragmentScope
public class LocationService implements Location.Service,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        ActivityCompat.OnRequestPermissionsResultCallback {

    private static final String TAG = LocationService.class.getSimpleName();
    private static final int PERMISSIONS_REQUEST = 13;

    private GoogleApiClient mGoogleApiClient;

    private Activity mActivity;

    private android.location.Location mLastLocation;

    private Location.Receiver mReceiver;

    @Inject
    LocationService(Activity activity) {

        mActivity = activity;
    }

    @Override
    public void getLastKnownLocation() {

        if (isPermissionsGranted(true))
            getLocation();

    }

    /**
     * @param request if permissions aren't granted and {@param request} is true,
     *                then request permissions
     * @return true if location permissions are granted
     */
    private boolean isPermissionsGranted(boolean request) {

        if (ActivityCompat.checkSelfPermission(mActivity,
                Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(mActivity,
                        Manifest.permission.ACCESS_COARSE_LOCATION) !=
                        PackageManager.PERMISSION_GRANTED) {

            if (request) {
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION},
                        PERMISSIONS_REQUEST);
            }
            return false;
        }
        return true;
    }

    private void getLocation() {

        if (mGoogleApiClient != null)

            mLastLocation = LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) {

            LatLng latLng = new LatLng(mLastLocation.getLatitude(),
                    mLastLocation.getLongitude());
            mReceiver.lastKnownLocation(latLng);
        } else {

            Log.e(TAG, "NULLLLLLLLLLLLLLLLLLLLLLL");
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {


    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void setupReceiver(Location.Receiver receiver) {

        mReceiver = receiver;
    }

    @Override
    public void onStart() {

        if (mGoogleApiClient != null){

            mGoogleApiClient.connect();
        } else {

            mGoogleApiClient = getGoogleApiClient();
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onStop() {

        if (mGoogleApiClient != null)
            mGoogleApiClient.disconnect();
    }

    private GoogleApiClient getGoogleApiClient(){

        return new GoogleApiClient.Builder(mActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        switch (requestCode){

            case PERMISSIONS_REQUEST:

                if (grantResults.length > 0 &&
                        grantResults[0] == PackageManager.PERMISSION_GRANTED){

                    getLastKnownLocation();
                } else {

                }
        }
    }
}

我需要以某种方式检查 mGoogleApiClient 是否已连接 (mGoogleApiClient.isConnected()),如果未连接,请等待连接,然后从 FusedLocationApi 获取位置,但我不想将方法放入 onConnected(),因为有时候会return定位,不想的时候会return定位。

你在评论区的解释后,我会做这样的事情:

  1. 在 Receiver/Fragment class 我会在 LocationService class 中放置一些将变量 "updateUI" 设置为 true 的逻辑,当它是适合 onConnected 调用 mReceiver.lastKnownLocation(latLng) 方法。默认值为 false,如果在接收器准备好之前调用 onConnected,方法 mReceiver.lastKnownLocation(latLng) 不会被调用。

  2. 另一种方法是将最后一个已知位置始终存储在您的 SharedPreferences 中(或至少在 onPause 方法中)。然后,您总是可以在第一次需要位置时使用它,并等待稍后更精确的位置,但这种方法在开始时不会那么精确。

所以,过了一段时间,我设法做到了,也完成了我的全部工作 class,我想和你分享我做了什么

public interface Location {

    interface Service extends BaseLocation<Receiver>{

        void onStart();

        void onStop();

        void onActivityResult(int requestCode, int resultCode);

        void getLastKnownLocation();
    }

    interface Receiver{

        void lastKnownLocation(double latitude, double longitude);

        void userRefusedToSendLocation();

        void unableToObtainLocation();
    }
}



import android.Manifest;
import android.app.Activity;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;

import javax.inject.Inject;

import pl.pancor.android.air.base.FragmentScope;

@FragmentScope
public class LocationService implements Location.Service,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        ActivityCompat.OnRequestPermissionsResultCallback, LocationListener,
        ResultCallback<LocationSettingsResult>{

    private static final int PERMISSIONS_REQUEST = 13;
    private static final int SETTINGS_CHECK = 23;
    private static final int GOOGLE_API_CLIENT_ERROR = 33;

    private static final int LOCATION_EXPIRATION_TIME = 10 * 1000;
    private static final int LOCATION_INTERVAL = 2 * 1000;

    private GoogleApiClient mGoogleApiClient;

    private Activity mActivity;

    private LocationRequest mLocationRequest;
    private android.location.Location mLastLocation;

    private Location.Receiver mReceiver;

    private Handler mHandler;
    private final Runnable mExpiredLocationUpdate = new Runnable() {
        @Override
        public void run() {
            mReceiver.unableToObtainLocation();
        }
    };

    private boolean isWaitingForConnect = false;

    @Inject
    LocationService(Activity activity) {

        mActivity = activity;
    }

    @Override
    public void getLastKnownLocation() {

        if (isPermissionsGranted(true))
            checkLocationSettings();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode) {

        resolveProblems(requestCode, resultCode);
    }

    @Override
    public void onLocationChanged(android.location.Location location) {

        if (mLastLocation == null) {

            mLastLocation = location;
            sendLatLngToReceiver();
        }

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mHandler.removeCallbacks(mExpiredLocationUpdate);
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        if (isWaitingForConnect)
            getLastKnownLocation();
    }

    @Override
    public void onConnectionSuspended(int i) {

        //mGoogleApiClient will automatically try to reconnect
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult result) {

        if (!result.hasResolution()){
            mReceiver.unableToObtainLocation();
            GoogleApiAvailability.getInstance()
                    .getErrorDialog(mActivity, result.getErrorCode(), 0).show();
            return;
        }
        if (mActivity.hasWindowFocus()) {
            try {
                result.startResolutionForResult(mActivity, GOOGLE_API_CLIENT_ERROR);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setupReceiver(Location.Receiver receiver) {

        mReceiver = receiver;
    }

    @Override
    public void onStart() {

        mHandler = new Handler();

        if (mGoogleApiClient != null){

            mGoogleApiClient.connect();
        } else {

            mGoogleApiClient = getGoogleApiClient();
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onStop() {

        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    mGoogleApiClient, this);

            mGoogleApiClient.disconnect();
        }
        mHandler.removeCallbacks(mExpiredLocationUpdate);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        switch (requestCode){
            case PERMISSIONS_REQUEST:

                if (grantResults.length > 0 &&
                        grantResults[0] == PackageManager.PERMISSION_GRANTED){

                    getLastKnownLocation();
                } else {

                    mReceiver.userRefusedToSendLocation();
                }
        }
    }

    @Override
    public void onResult(@NonNull LocationSettingsResult result) {

        final Status status = result.getStatus();
        switch (status.getStatusCode()){
            case LocationSettingsStatusCodes.SUCCESS:

                getLocation();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                if (mActivity.hasWindowFocus()) {
                    try {
                        status.startResolutionForResult(mActivity, SETTINGS_CHECK);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case  LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                mReceiver.unableToObtainLocation();
                break;
        }
    }

    private void checkLocationSettings() {

        if (mGoogleApiClient != null){

            mLocationRequest = new LocationRequest()
                    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                    .setFastestInterval(LOCATION_INTERVAL / 2)
                    .setInterval(LOCATION_INTERVAL)
                    .setNumUpdates(1);

            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(mLocationRequest);

            PendingResult<LocationSettingsResult> result = LocationServices
                    .SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
            result.setResultCallback(this);
        }
    }

    private void getLocation(){

        if (mGoogleApiClient != null)
            mLastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);

        sendLatLngToReceiver();
    }

    private void sendLatLngToReceiver(){

        if (mLastLocation != null) {

            mReceiver.lastKnownLocation(mLastLocation.getLatitude(),
                    mLastLocation.getLongitude());
            mHandler.removeCallbacks(mExpiredLocationUpdate);
        } else {

            requestLocation();
        }
    }

    private void requestLocation(){

        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {

            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
            mHandler.postDelayed(mExpiredLocationUpdate, LOCATION_EXPIRATION_TIME);
        } else {

            isWaitingForConnect = true;
        }
    }

    /**
     * @param request if permissions aren't granted and {@param request} is true,
     *                then request permissions
     * @return true if location permissions are granted
     */
    private boolean isPermissionsGranted(boolean request) {

        if (ActivityCompat.checkSelfPermission(mActivity,
                Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(mActivity,
                        Manifest.permission.ACCESS_COARSE_LOCATION) !=
                        PackageManager.PERMISSION_GRANTED) {

            if (request) {
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION},
                        PERMISSIONS_REQUEST);
            }
            return false;
        }
        return true;
    }

    private GoogleApiClient getGoogleApiClient(){

        return new GoogleApiClient.Builder(mActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    private void resolveProblems(int requestCode, int resultCode){

        switch (requestCode){
            case SETTINGS_CHECK:
                switch (resultCode){
                    case Activity.RESULT_OK:
                        getLastKnownLocation();
                        break;
                    case Activity.RESULT_CANCELED:
                        mReceiver.userRefusedToSendLocation();
                        break;
                }
                break;
            case GOOGLE_API_CLIENT_ERROR:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        mGoogleApiClient.connect();
                        break;
                    case Activity.RESULT_CANCELED:
                        mReceiver.unableToObtainLocation();
                        break;
                }
        }
    }
}