我的应用程序需要精确的用户位置,因为它需要用户在特定位置执行任务。我已经遵循了文档来正确地请求同时具有
我遇到的问题是我的用户没有被提示允许精确位置。
我期望看到文档中的图3中的提示。 图3:当您的应用程序在单个运行时请求
相反,我从文档中看到了图2中的提示。 图2。当您的应用程序仅请求“ACCESS_COARSE_LOCATION”权限时出现的系统权限对话框。
此外,我期望当我已经拥有“COARSE”权限并再次请求“FINE”和“COARSE”权限时,我应该看到“升级到精确定位权限”对话框,但是我根本没有看到提示。
我已在Pixel 3a(物理设备和模拟器)上进行了测试,还收到了其他用户在运行Android 12的各种Pixel和三星Galaxy设备上的报告。
当我们在build.gradle中使用compileSdk 30和targetSdkVersion 30时,问题不存在,但我们需要能够支持新功能和新版本的Android(并且能够升级到仅支持使用较新SDK版本构建的依赖项)。
为什么权限对话框没有正确显示?
ACCESS_FINE_LOCATION
和ACCESS_COARSE_LOCATION
权限的适当方法。class LocationChooserFragment : BottomSheetDialogFragment() {
// only relevant parts shown
/**
* A utility class that holds all the code for requesting location updates,
* so that we can re-use it in multiple fragments.
*/
private var locationTracker: LocationTracker? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// all the other initialization for viewbinding
tryToGetLocationUpdates()
}
private fun tryToGetLocationUpdates() {
val request = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true && permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true) {
requestLocationUpdates()
}
}
when {
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED -> {
Timber.i("Already have required permissions")
requestLocationUpdates()
}
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) ||
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION) -> {
Timber.i("Showing permission rationale dialog")
// A wrapper to show a dialog that explains what we need permissions for
alertDialog(
title = getString(R.string.permissions),
msg = getString(R.string.why_we_need_location),
onClick = { yes ->
if (yes) {
request.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
}
)
}
else -> {
Timber.i("Prompting for location permissions")
request.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
}
}
private fun requestLocationUpdates() {
if (locationProvider == null) {
locationProvider = LocationProvider(requireContext())
}
locationProvider?.requestLocationUpdates()
}
}
我遇到的问题是我的用户没有被提示允许精确位置。
我期望看到文档中的图3中的提示。 图3:当您的应用程序在单个运行时请求
ACCESS_FINE_LOCATION
和ACCESS_COARSE_LOCATION
权限时出现的系统权限对话框。相反,我从文档中看到了图2中的提示。 图2。当您的应用程序仅请求“ACCESS_COARSE_LOCATION”权限时出现的系统权限对话框。
此外,我期望当我已经拥有“COARSE”权限并再次请求“FINE”和“COARSE”权限时,我应该看到“升级到精确定位权限”对话框,但是我根本没有看到提示。
我已在Pixel 3a(物理设备和模拟器)上进行了测试,还收到了其他用户在运行Android 12的各种Pixel和三星Galaxy设备上的报告。
当我们在build.gradle中使用compileSdk 30和targetSdkVersion 30时,问题不存在,但我们需要能够支持新功能和新版本的Android(并且能够升级到仅支持使用较新SDK版本构建的依赖项)。
为什么权限对话框没有正确显示?
targetSdkVersion
升级到31之后是吗? - Pawel