权限被拒绝无法获取位置Android

时间:2018-06-05 10:42:27

标签: android geolocation compass-geolocation ip-geolocation google-geolocation

我在初始设置时向Android上的应用程序提供权限 跟踪地理位置和移动lacs,小区ID和国家代码以及塔码。我在中途收到以下错误 - “权限被拒绝无法获取Android的位置”,即最初在Android上安装应用程序时,它首先提供地理位置,但在中途停止获取地理位置。

上述解决方案将非常受欢迎

请在下面找到代码 -

private void requestStoragePermission() {
    Dexter.withActivity(this)
            .withPermissions(
                    Manifest.permission.WAKE_LOCK,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION)
            .withListener(new MultiplePermissionsListener() {
                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) {
                    // check if all permissions are granted
                    if (report.areAllPermissionsGranted()) {
                        Toast.makeText(getApplicationContext(), "All permissions are granted!", Toast.LENGTH_SHORT).show();
                    }

                    // check for permanent denial of any permission
                    if (report.isAnyPermissionPermanentlyDenied()) {
                        // show alert dialog navigating to Settings
                        showSettingsDialog();
                    }
                }

                @Override
                public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                    token.continuePermissionRequest();
                }
            }).
            withErrorListener(new PermissionRequestErrorListener() {
                @Override
                public void onError(DexterError error) {
                    Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
                }
            })
            .onSameThread()
            .check();
}

1 个答案:

答案 0 :(得分:1)

获取位置数据然后需要运行时权限尝试下面的代码..

如果不存在,则将内容添加到您的应用中,以及如何处理运行时权限。

尝试这样做,确保您的应用程序级别gradle文件在编译sdk版本最新。像

 compileSdkVersion 27

然后还在android清单文件中添加权限。

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

并在检查运行时权限后如下所示制作两种方法。

/** 
 * this method check permission and return current state of permission need. 

 */ 
private boolean checkPermissions() { 
int permissionState = ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION); 
return permissionState == PackageManager.PERMISSION_GRANTED;
 } 

/** 
* this method request to permission asked. 
*/ 
private void requestPermissions() { 
 boolean shouldProvideRationale =
        ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION); 

if (shouldProvideRationale) {
    Log.i(TAG, "Displaying permission rationale to provide additional context.");
} else { 
    Log.i(TAG, "Requesting permission");
    // previously and checked "Never ask again". 
    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            REQUEST_PERMISSIONS_REQUEST_CODE); 
  }  
 } 

  /** 
  * Callback received when a permissions request has been completed. 
  */ 
  @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                   @NonNull int[] grantResults) {
Log.i(TAG, "onRequestPermissionResult");
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
    if (grantResults.length <= 0) {
        // If user interaction was interrupted, the permission request is cancelled and you 
        // receive empty arrays. 
        Log.i(TAG, "User interaction was cancelled.");
    } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Permission was granted. Kick off the process of building and connecting 
        // GoogleApiClient. 
        buildGoogleApiClient(); 
    } else { 
        // Permission denied. 

    } 
} 
} 

然后当调用get location方法然后像下面检查一样 在onCreateview方法

 // Check if the user revoked runtime permissions.
if (!checkPermissions()) {
    requestPermissions();
}

buildGoogleApiClient();