用户权限检查仅在安装后首次使用

时间:2018-08-22 10:13:42

标签: android kotlin permissions user-permissions

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

checkPermission:

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
        }
    }
}

当我单击图标时,我在checkPermission上调用onStart函数。

3 个答案:

答案 0 :(得分:2)

您将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.
    }
});

答案 1 :(得分:0)

    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
}

答案 2 :(得分:-1)

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