Permission Handle
Permission Check/Request
Section titled “Permission Check/Request”
⚠️ Requires Android Studio version above
TIRAMISU:@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Death simple way
Section titled “Death simple way”ActivityCompat.requestPermissions( this, arrayOf(Manifest.permission.CAMERA), 1)Request permission
Section titled “Request permission”Example: Check whether user allows APP to push notification
val hasPermission = ContextCompat.checkSelfPermission( context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTEDCreate 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()}Request permission if not granted
Section titled “Request permission if not granted”if (!hasPermission) launcher.launch(Manifest.permission.POST_NOTIFICATIONS)Request multiple permissions
Section titled “Request multiple permissions”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))Rejection handling
Section titled “Rejection handling”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)