Сбои без каких-либо ошибок в logcat, при проверке разрешения - PullRequest
0 голосов
/ 13 ноября 2018

Я хочу проверить разрешение для местоположения, но во время выполнения приложение вылетает без каких-либо ошибок. Так что строка, которая вызывает ошибку, не отображается. Пожалуйста, предложите мне изменить или решить эту проблему. Я написал этот код во фрагменте.

Заранее спасибо.

функция, которая проверяет разрешение

String[] permissionsRequired = new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION};


private void checkPermission() {

    if (ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(getContext(), permissionsRequired[1]) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[0])
                || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permissionsRequired[1])) {

            ActivityCompat.requestPermissions(getActivity(), permissionsRequired, PERMISSION_CALLBACK_CONSTANT);
        }


    } else {

    }

}

1 Ответ

0 голосов
/ 13 ноября 2018
 @Override
public void onConnected(@Nullable Bundle bundle) {
    checkPermissions();
}



 private void checkPermissions(){
    int permissionLocation = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION);
    List<String> listPermissionsNeeded = new ArrayList<>();
    if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this,
                    listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
        }
    }else{
        getMyLocation();
    }

}





private void getMyLocation(){
    if(googleApiClient!=null) {
        Log.e("TAg", "getMyLocation: " + "goolenot null" );
        if (googleApiClient.isConnected()) {
            Log.e("TAG", "getMyLocation: " + "connectd" );
            int permissionLocation = ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION);
            if (permissionLocation == PackageManager.PERMISSION_GRANTED) {

                LocationRequest locationRequest = new LocationRequest();
                locationRequest.setInterval(1000);
                locationRequest.setFastestInterval(1000);
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                        .addLocationRequest(locationRequest);
                builder.setAlwaysShow(true);
            //    map_view.getMapAsync(this);
                LocationServices.FusedLocationApi
                        .requestLocationUpdates(googleApiClient, locationRequest, this);
                PendingResult result =
                        LocationServices.SettingsApi
                                .checkLocationSettings(googleApiClient, builder.build());
                result.setResultCallback(new ResultCallback() {

                                             @Override
                                             public void onResult(@NonNull Result result) {
                                                 final Status status = result.getStatus();
                                                 switch (status.getStatusCode()) {
                                                     case LocationSettingsStatusCodes.SUCCESS:
                                                         // All location settings are satisfied.
                                                         // You can initialize location requests here.
                                                         int permissionLocation = ContextCompat
                                                                 .checkSelfPermission(MainActivity.this,
                                                                         Manifest.permission.ACCESS_FINE_LOCATION);
                                                         if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
                                                         //    lastLocation = LocationServices.FusedLocationApi
                                                          //           .getLastLocation(googleApiClient);
                                                             setFragmentoriginal(new HomeFragment());
                                                             changePositiono(0);
                                                         }
                                                         break;
                                                     case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                                         // Location settings are not satisfied.
                                                         // But could be fixed by showing the user a dialog.
                                                         try {
                                                             // Show the dialog by calling startResolutionForResult(),
                                                             // and check the result in onActivityResult().
                                                             // Ask to turn on GPS automatically
                                                             status.startResolutionForResult(MainActivity.this,
                                                                     REQUEST_CHECK_SETTINGS_GPS);
                                                         } catch (IntentSender.SendIntentException e) {
                                                             // Ignore the 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.
                                                         //finish();
                                                         break;
                                                 }
                                             }
                                         }

                );
            }
        }
    }
}





  @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    int permissionLocation = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);
    if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
        getMyLocation();
    }
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS_GPS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    getMyLocation();
                    break;
                case Activity.RESULT_CANCELED:
                    finish();
                    break;
            }
            break;
    }
}





private final static int REQUEST_CHECK_SETTINGS_GPS = 0x1;
private final static int REQUEST_ID_MULTIPLE_PERMISSIONS = 0x2;

наслаждайся

...