【问题标题】:Marshmallow - Google Maps Runtime permission issueMarshmallow - Google Maps Runtime 权限问题
【发布时间】:2016-06-27 18:56:44
【问题描述】:

我正在使用 Google 位置 API 在地图上显示用户位置。

当应用首次加载时,系统会显示基本原理对话框,解释用户需要启用位置访问权限的原因。然后,显示runtime permission dialog,用户点击“允许”以启用位置访问。然后地图加载正常,没有任何崩溃。

但是,当应用程序恢复时(进入后台后),由于用户已授予位置访问权限,因此不会出现任何理由对话框。在这种情况下,仅出现 runtime permission dialog。现在如果用户点击“Deny”,应用就会崩溃。此代码在低于 M 的 Android 版本中运行良好(Lollipop、Jellybean、KitKat 等

现阶段有没有办法处理运行时异常?

错误是:

java.lang.SecurityException: 客户端必须有 ACCESS_FINE_LOCATION 请求 PRIORITY_HIGH_ACCURACY 位置的权限。

我正在使用没有任何第三方库的标准 Sample MyLocation 演示应用:

GoogleMaps MapDemo on Github

我也尝试过使用 PermissionsDispatcher 库,但同样的错误仍然存​​在。

PermissionDispatcher Android-Google-Maps-Demo

private void enableMyLocation() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission to access the location is missing.
        PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                Manifest.permission.ACCESS_FINE_LOCATION, true);
    } else if (mMap != null) {
        // Access to the location has been granted to the app.
        mMap.setMyLocationEnabled(true);
    }
}

非常感谢任何帮助。

【问题讨论】:

  • 请在您请求权限的地方发布代码
  • 接受/拒绝权限的实现并不意味着你的代码可以正常运行。这仅取决于您在这些情况下编写的代码。请发布 onCreate() 和请求权限回调方法?
  • 我正在使用link中给出的确切示例
  • 当活动恢复并且运行时权限对话框显示在行时,if语句返回false mMap.setMyLocationEnabled(true);线。如果我在权限对话框中选择“拒绝”,应用程序就会崩溃。此时不会调用回调方法。

标签: android google-maps google-play-services android-6.0-marshmallow


【解决方案1】:

确保您在 Manifest 文件中添加了正确的权限,说明您所要求和确切需要的权限

您的代码可能有问题。 试试这个库:RuntimePermission Library 手动请求权限。

【讨论】:

    【解决方案2】:

    从 Android 6.0(API 级别 23)开始,用户grant permissions 在应用运行时访问应用,而不是在安装应用时。这种方法简化了应用程序安装过程,因为用户在安装或更新应用程序时不需要授予权限。它还让用户可以更好地控制应用的功能。

    如何查看是否有权限:

    // Assume thisActivity is the current activity
    int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.ACCESS_FINE_LOCATION);
    How to request the permissions you need :
    
    if (ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {
    
    ActivityCompat.requestPermissions(thisActivity,
    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
    MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
    
    // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
    // app-defined int constant. The callback method gets the
    // result of the request.
    }
    }
    

    如何访问请求的结果:

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0
    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
    // permission was granted, yay! Do the task you need to do.
    
    } else {
    
    // permission denied, boo! Disable the functionality that depends on this permission.
    }
    return;
    }
    }
    }
    

    这是一个相关的 SO 票:Google Maps API Android v2 - "ACCESS_FINE_LOCATION" permission required with my-location layer enabled

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多