java - android.permission.ACCESS_FINE_LOCATION 还向 android.permission.ACCESS_COARSE_LOCATION 授予权限

标签 java android kotlin

我正在测试相机和位置的 Android 权限。令我惊讶的是,我了解到当我授予精细位置访问权限时,系统也会授予粗略位置访问权限。这是正常的吗?我首先检查 Toasts 以查看授予的访问权限。当我仅授予粗略位置访问权限时,它显示精细位置权限被拒绝,这是正常的。但是当我授予对精细地址的访问权限时,它也授予对粗略位置的权限。这正常吗?

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var cameraResultLauncher: ActivityResultLauncher<String> =
        registerForActivityResult(
        ActivityResultContracts.RequestPermission()){
            isGranted ->
            if (isGranted){
                Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show()
            }
        }

    //This time we create the Activity result launcher of type Array<String>
    private var cameraAndLocationResultLauncher: ActivityResultLauncher<Array<String>> =
        registerForActivityResult(
            ActivityResultContracts.RequestMultiplePermissions()){
            permissions ->
            /**
            Here it returns a Map of permission name as key with boolean as value
            Loop through the map to get the value we need which is the boolean
            value
             */
            permissions.entries.forEach{
                val permissionName = it.key
                val isGranted = it.value
                println(permissionName)
                println(isGranted)
                if (isGranted){
                    if (permissionName == Manifest.permission.ACCESS_FINE_LOCATION){
                        Toast.makeText(this, "Permission granted for fine location", Toast.LENGTH_LONG).show()

                    }else if(permissionName == Manifest.permission.ACCESS_COARSE_LOCATION){
                        Toast.makeText(this, "Permission granted for coarse location", Toast.LENGTH_LONG).show()

                    }else{
                        Toast.makeText(this, "Permission granted for Camera", Toast.LENGTH_LONG).show()
                    }
                }else{
                    if (permissionName == Manifest.permission.ACCESS_FINE_LOCATION){
                        Toast.makeText(this, "Permission denied for fine location", Toast.LENGTH_LONG).show()

                    }else if(permissionName == Manifest.permission.ACCESS_COARSE_LOCATION){
                        Toast.makeText(this, "Permission denied for coarse location", Toast.LENGTH_LONG).show()

                    }else{
                        Toast.makeText(this, "Permission denied for Camera", Toast.LENGTH_LONG).show()
                    }
                }
            }
        }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val btnCameraPermission: Button = binding.btnCameraPermission
        btnCameraPermission.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && shouldShowRequestPermissionRationale(
                    Manifest.permission.CAMERA)){
                showRationaleDialog("Permission Demo requires camera access", "Camera cannot be used because Camera access is denied")
            }else{
                // You can directly ask for the permission.
                // The registered ActivityResultCallback gets the result of this request.

//                cameraResultLauncher.launch(Manifest.permission.CAMERA)  this for only camera permission
                cameraAndLocationResultLauncher.launch(arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION))
            }
        }
    }

    private fun showRationaleDialog(
        title: String,
        message: String,
    ){
        val builder: AlertDialog.Builder = AlertDialog.Builder(this)
        builder.setTitle(title)
            .setMessage(message)
            .setPositiveButton("Cancel"){
                    dialog, _-> dialog.dismiss()
            }
        builder.create().show()
    }

}

I/System.out: android.permission.ACCESS_FINE_LOCATION
I/System.out: true
I/System.out: android.permission.ACCESS_COARSE_LOCATION
I/System.out: true

最佳答案

Is this normal?

是的。大多数权限不是这样设置的,但是这对权限是这样设置的。毕竟,如果您可以测量几十米内的位置 (ACCESS_FINE_LOCATION),那么根据定义,您可以测量更大半径内的位置 (ACCESS_COARSE_LOCATION)。

关于java - android.permission.ACCESS_FINE_LOCATION 还向 android.permission.ACCESS_COARSE_LOCATION 授予权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74187738/

相关文章:

java - GridLayout 中的 JLabel 并不总是被绘制

Java 数字/数学抽象

java - 如何在 MPAndroidChart 中获取 Horizo​​ntalBarChart 上点击栏的标签值?

android - 如何链接到 Android Market 应用程序

java - 如何在 Java 中使用 HttpGet 发送 cookie

java - Kotlin - 从 Java 转换时出现 IndexOutOfBoundsException

java - 运行读取Elasticsearch的map-reduce作业时出错

android - 为什么谷歌播放服务项目为什么不使用 google-play-services.jar

regex - 使用多字符正则表达式模式进行分割并保留分隔符

functional-programming - Kotlin Either monad:重构构造函数调用以处理CPS