我正在使用Google位置API来显示用户在地图上的位置。
当应用程序第一次加载时,显示理由对话框,解释用户为什么需要启用位置访问。然后显示runtime permission dialog
,用户点击"Allow"开启位置访问。然后Map加载正常,没有任何崩溃。
然而,当应用程序恢复(后进入后台),没有理由对话框出现,因为用户已经授予了位置访问权。在此场景中,只出现runtime permission dialog
。现在,如果用户点击"拒绝",应用程序崩溃。此代码在M以下的Android版本(Lollipop, Jellybean, KitKat等)
是否有一种方法来处理这个阶段的运行时异常?
错误是:
. lang。SecurityException: Client必须有ACCESS_FINE_LOCATION权限请求PRIORITY_HIGH_ACCURACY位置
我使用标准的样本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);
}
}
请确保您在Manifest文件中添加了正确的权限,以确定您正在请求和需要的权限
可能是你的代码有问题。试试这个库:RuntimePermission库手动请求权限。