Skip to content

Permission Handle

Permission Dialog

⚠️ Requires Android Studio version above TIRAMISU: @RequiresApi(Build.VERSION_CODES.TIRAMISU)

ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
1
)

Example: Check whether user allows APP to push notification

val hasPermission = ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED

Create a launcher to show the system dialog for requesting permission

Section titled “Create a launcher to show the system dialog for requesting permission”
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) {
Toast.makeText(context, "A permission request has done", Toast.LENGTH_SHORT).show()
}
if (!hasPermission) launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
val multiplePermissionsLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
val cameraGranted = permissions[Manifest.permission.CAMERA] == true
val locationGranted = permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true
}
multiplePermissionsLauncher.launch(
arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
)

if the user denies previously show a dialog before ask again

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// Show a dialog explaining why you need it, then request again
} else {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}

If the user denies the permission twice, they will need to go to the app settings to manually grant it. We can use the intent method below to automatically open it for them.

val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", packageName, null)
}
startActivity(intent)