用户权限检查仅在安装后第一次有效

User permission check only works first time after installation

我的应用程序仅在安装应用程序后第一次显示权限对话框。如果我关闭它并再次打开,它不会显示请求 window。即使我在 clickEvent 上手动禁用位置权限或请求权限 window,它也不起作用。

检查权限:

private fun checkPermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //permission not granted

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION)) {
            //show an explanation to the user
        } else {
            //request the permission
            ActivityCompat.requestPermissions(this,
                    arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                    ACCESS_COARSE_LOCATION_CODE)
        }
    } else {
        ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                ACCESS_COARSE_LOCATION_CODE)
    }
}

onRequestPermissionsResult:

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    when(requestCode){
        ACCESS_COARSE_LOCATION_CODE -> {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                //granted
            } else {
                //denied
            }
            return
        }
    }
}

我在 onStart 和单击图标时调用 checkPermission 函数。

您将 shouldShowRequestPermissionRationale 部分留空,因此它没有第二次请求许可。在其中显示一个警告对话框,为什么您需要权限以及当用户单击“确定”时再次请求权限。作为替代,您可以直接再次请求许可。

private fun checkPermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //permission not granted

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION)) {
             ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                ACCESS_COARSE_LOCATION_CODE)
        } else {
            //request the permission
            ActivityCompat.requestPermissions(this,
                    arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                    ACCESS_COARSE_LOCATION_CODE)
        }
    } else {
        ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                ACCESS_COARSE_LOCATION_CODE)
    }
}

您没有正确处理拒绝和不再询问的情况,为了简单起见,我建议您使用 this 库。您将在一个地方收到所有回电

  Permissions.check(this, Manifest.permission.ACCESS_COARSE_LOCATION, null, new PermissionHandler() {
    @Override
    public void onGranted() {
        // do your task.
    }
});

当用户接受您的许可时,它会永久接受(除非用户将其删除)检查权限仅在尚未授予权限时显示。当用户通过设置->应用设置->选择的应用->权限清除数据或取消勾选权限时。

    private fun checkAndRequestPermissions(): Boolean {

    val permissionContact = ContextCompat.checkSelfPermission(activity,
            Manifest.permission.READ_CONTACTS)

    val permissionPhoneState = ContextCompat.checkSelfPermission(activity,
            Manifest.permission.READ_PHONE_STATE)
    val permissionCamera = ContextCompat.checkSelfPermission(activity,
            Manifest.permission.CAMERA)
    val permissionWriteExternal = ContextCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)

    val locationPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
    val listPermissionsNeeded = java.util.ArrayList<String>()
    when {
        locationPermission != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION)
    }

    when {
        permissionContact != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS)
    }

    when {
        permissionPhoneState != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE)
    }
    when {
        permissionCamera != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.CAMERA)
    }
    when {
        permissionWriteExternal != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }
    when {
        !listPermissionsNeeded.isEmpty() -> ActivityCompat.requestPermissions(activity, listPermissionsNeeded.toTypedArray(), REQUEST_ID_MULTIPLE_PERMISSIONS)
    }
    return true
}