等待连接的呼叫,以获取位置



所以,我有自定义位置服务类,我想从中获得最后已知的位置。可能,我可以在连接GoogleapicLient之前调用getLastKnownLocation(),因此我必须等待它然后致电getLastKnownLocation(),但是我不知道如何管理它。我认为 rxjava 2 可以为我提供帮助,但我还不熟悉该框架。这是我现在的班级:

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(),因为有时会返回位置,何时会返回位置我不想返回位置。

在您在评论部分中的解释之后,我会做这样的事情:

  1. 在接收器/片段类中,我会在" updateui'update"的位置类" updateui"类中放置一些逻辑,当时在conservice类中,当时可以连接调用 mReceiver.lastKnownLocation(latLng)方法。默认值将是false,如果在接收器准备就绪之前被拨打连接,则方法mReceiver.lastKnownLocation(latLng)不会被称为。

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

所以,一段时间后,我设法完成并完成了整个班级,我想与您分享,我做了什么

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

相关内容

  • 没有找到相关文章

最新更新