onActivityResult resultCode всегда равен нулю для запроса местоположения GoogleApiClient - PullRequest
0 голосов
/ 24 апреля 2019

Почему resultCode из onActivityResult для Запрос местоположения при использовании Google API Client всегда ноль?

Мобильное приложение Android, подключающееся к API-клиенту:

final static int REQUEST_LOCATION =2001;
        private void getGoogleApiClient() {        
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        }

Определение запроса местоположения:

        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(30000);
            mLocationRequest.setFastestInterval(10000);

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            Loc_Update();
        }

Запрос Местоположение с использованием клиента Google APi


        private void Loc_Update() {
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(mLocationRequest)
                    .setAlwaysShow(true);
            Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
            task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
                @Override
                public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
                    try {
                        LocationSettingsResponse response = task.getResult(ApiException.class);
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                            mFusedLocationClient.requestLocationUpdates(mLocationRequest,new LocationCallback(){
                                @Override
                                public void onLocationResult(LocationResult locationResult) {
                                    for (Location location : locationResult.getLocations()) {                                    
                                        //Do what you want with location
                                        //like update camera
                                       // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16f));
                                    }
                                }
                            },null);
                        }
                    } catch (ApiException exception) {
                        switch (exception.getStatusCode()) {
                            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                // Location settings are not satisfied. But could be fixed by showing the
                                // user a dialog.
                                try {
                                    // Cast to a resolvable exception.
                                    ResolvableApiException resolvable = (ResolvableApiException) exception;
                                    // Show the dialog by calling startResolutionForResult(),
                                    // and check the result in onActivityResult().
                                    resolvable.startResolutionForResult(MainActivity.this, 2001);
                                    break;
                                } catch (IntentSender.SendIntentException e) {
                                    // Ignore the error.
                                } catch (ClassCastException e) {
                                    // Ignore, should be an impossible error.
                                }
                                break;
                            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                                // Location settings are not satisfied. However, we have no way to fix the
                                // settings so we won't show the dialog.
                                break;
                        }
                    }}
            });
            task.addOnCanceledListener(new OnCanceledListener() {
                @Override
                public void onCanceled() {

                }
            });
        }

        //GoogleApiClient.ConnectionCallbacks
        public void onConnected(Bundle bundle) {
            createLocationRequest();
        }

        @Override
        public void onConnectionSuspended(int i) {

        }
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            Toast.makeText(getApplicationContext(),"Failed to connect ",Toast.LENGTH_SHORT);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            try {            
                switch (requestCode)
                {
                    case REQUEST_LOCATION:
                        switch (resultCode)
                        {
                            case Activity.RESULT_OK:
                            {
                                Toast.makeText(getApplicationContext(),"user responded yes ",Toast.LENGTH_SHORT);
                                break;
                            }
                            case Activity.RESULT_CANCELED:
                            {
                                Toast.makeText(getApplicationContext(),"user responded No ",Toast.LENGTH_SHORT);
                                break;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

При нажатии «ОК», resultCode должно быть -1 и «Нет, спасибо», resultCode должно быть 0

...