在请求Fragment中的权限时,我目前遇到问题。要求位置权限时,未调用我片段中的onRequestPermissions
方法。
这里是我的代码。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity?.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestLocationPermission()
permissionNeededLayout.visibility = View.VISIBLE
}
if (activity?.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
permissionNeededLayout.visibility = View.GONE
permissionDeniedLayout.visibility = View.GONE
}
} else {
//TODO
}
private fun requestLocationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val shouldProvideRationale = shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
if (shouldProvideRationale) {
//TODO show need location fragment
Log.d("ProductListFragment", "Displaying permission rationale to provide additional context.")
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST)
} else {
Log.d("ProductListFragment", "Not displaying rational")
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST)
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Log.d(TAG,"onRequestPermissionsResult")
if (requestCode == LOCATION_PERMISSION_REQUEST) {
Log.d(TAG,"result = result location")
when {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
grantResults.isEmpty() -> Log.d(TAG, "on result User interaction was cancelled.")
//Permission granted.
(grantResults[0] == PackageManager.PERMISSION_GRANTED) -> Log.d(TAG,"on result permission granted, get location")
// Permission denied.
// Additionally, it is important to remember that a permission might have been
// rejected without asking the user for permission (device policy or "Never ask
// again" prompts). Therefore, a user interface affordance is typically implemented
// when permissions are denied. Otherwise, your app could appear unresponsive to
// touches or interactions which have required permissions.
(grantResults[0] == PackageManager.PERMISSION_DENIED) -> Log.d(TAG,"on result permission denied")
else -> Log.d(TAG,"grantResult ${grantResults.size}")
}
Log.d(TAG,"onPermisionResult no location request code")
}
}
似乎创建了我创建片段的Activity的onRequestPermissions方法,而不是调用了being方法。我想念什么吗?我在做什么错了?