Home > Software design >  How to notify the android user if camera permission is denied
How to notify the android user if camera permission is denied

Time:02-08

I am working with camera permissions. when the user clicks on take photo button, user will be shown with run time permissions menu and lets say user deny's it and then clicks on take photo button, run time permissions will be shown second time.

after this clicking the take photo button nothing happens.

What I want to do is, after the second attempt, i want to show a popup telling the user to go to settings to change the permissions.

How can I know if the user has denied the permission twice.

this is what I have coded so far

enter image description here

takePhotoBtn.setOnClickListener {
        takePhoto()
    }

private fun takePhoto() {
    activity?.let {
        if (isCameraPermissionsAllowed()) {
            capturePhoto()
        } else {
            permReqLauncher.launch(
                CAMERA_PERMISSION
            )
        }
    }
}

private val permReqLauncher =
    registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        val granted = permissions.entries.all {
            it.value == true
        }
        if (granted) {
            capturePhoto()
        }
    }


private fun capturePhoto() {
    onUtilityBillTypeListener.onUtilityBillTypePhotoLink(true)
}

where is the right place to add this dinielDialog

private fun showPermissionDeniedDialog() {
    AlertDialog.Builder(this.requireContext())
        .setTitle("Permission Denied")
        .setMessage("Permission is denied, Please allow permissions from App Settings.")
        .setPositiveButton("Settings",
            DialogInterface.OnClickListener { dialogInterface, i ->
                // send to app settings if permission is denied permanently
                val intent = Intent()
                intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                val uri = Uri.fromParts("package", getPackageName(this.requireContext()), null)
                intent.data = uri
                startActivity(intent)
            })
        .setNegativeButton("Cancel", null)
        .show()
}

CodePudding user response:

You could write something like this

if (isCameraPermissionsAllowed()) {
            capturePhoto()
        } else {
            if (permissionDeniedFlag) {
                showPermissionDeniedDialog()
            } else {
            permissionDeniedFlag = True
            permReqLauncher.launch(
                CAMERA_PERMISSION
            )
        }

With an initial declaration of var permissionDeniedFlag = False.

CodePudding user response:

On your code where you're getting permission result you can do it like this :

private val requestPermissionLauncher =
    registerForActivityResult(ActivityResultContracts.RequestPermission()) {
            isGranted: Boolean ->
        if (isGranted) //perform action
        else {
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("Permission Required!")
            builder.setMessage("We need permission in order to perform this action.")
            builder.setPositiveButton("OK") { dialog, _ ->
                showPermRationale()
                dialog.cancel()
            }
            builder.setNegativeButton("CANCEL") { dialog, _ ->
                dialog.cancel()
            }
            builder.show()
        }
    }

private fun showPermRationale() {
    val intent = Intent()
    intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
    val uri = Uri.fromParts(
        "package", requireActivity().packageName, null)
    intent.data = uri
    requireActivity().startActivity(intent)
}
  •  Tags:  
  • Related