我目前正在尝试将我的应用程序迁移到目标SDK 33。当我尝试请求位置权限(粗略或精细,总是同样的问题)时,它总是被拒绝,甚至没有显示请求对话框。
我已经在Manifest中包含了两个位置权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在Activity中,我注册了一个ResultContract:
private ActivityResultLauncher<String[]> locationPermissionRequest =
registerForActivityResult(new ActivityResultContracts
.RequestMultiplePermissions(), result -> {
Boolean fineLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_FINE_LOCATION, false);
Boolean coarseLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_COARSE_LOCATION, false);
if (fineLocationGranted != null && fineLocationGranted) {
// Precise location access granted.
Log.d(TAG, "permissions granted");
//Do Stuff here
} else if (coarseLocationGranted != null && coarseLocationGranted) {
// Only approximate location access granted.
Log.d(TAG, "Only approximate location access granted");
} else {
// No location access granted.
Log.e(TAG, "location permission denied");
}
}
);
在检查权限是否已被授予并向用户显示对话框之后,我请求权限:
if (ActivityCompat.checkSelfPermission(DashboardActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(DashboardActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ModalDialog permissionInfoDialog = null;
ModalDialog.Builder permissionInfoDialogBuilder = new ModalDialog.Builder(DashboardActivity.this);
permissionInfoDialogBuilder.setType(ModalDialog.Type.DEFAULT_INFO)
.setPositiveButton(android.R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "requesting location permission");
locationPermissionRequest.launch(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
});
}
})
.setTextNegative(android.R.string.cancel)
.setTitle(R.string.dashboard_request_location_permission_title)
.setMessage(R.string.dashboard_request_location_permission_text)
.setCloseable(false);
permissionInfoDialog = permissionInfoDialogBuilder.create();
permissionInfoDialog.show();
return;
}
以上代码复制自Android文档(https://developer.android.com/training/permissions/requesting#location),用于请求相机和蓝牙权限。不知何故,它不工作的位置权限。
在迁移到sdk33之前(之前是32),一切都很好。我还漏了什么吗根据文档,这应该有效。
提前问候和感谢
Herrmann_wst
2条答案
按热度按时间oewdyzsn1#
首先,你应该要求粗。
并且只有在用户同意请求FINE之后。
iugsix8n2#
我找到了解决方案:我的一个包含的模块在其清单中有以下行:
不知何故,即使我在没有sdk限制的情况下在应用程序清单中包含权限,android也不授予权限。看起来模块清单正在覆盖应用清单。