Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// <-------- MainActivity.kt ----------- >
private fun requestCameraPermission() {
requestCameraPermissionLauncher.launch( Manifest.permission.CAMERA )
}
private val requestCameraPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission() ) {
isGranted : Boolean ->
if ( isGranted ) {
setupCameraProvider()
}
else {
val alertDialog = AlertDialog.Builder( this ).apply {
setTitle( "Permissions" )
setMessage( "The app requires the camera permission to function." )
setPositiveButton( "GRANT") { dialog, _ ->
dialog.dismiss()
requestCameraPermission()
}
setNegativeButton( "CLOSE" ) { dialog, _ ->
dialog.dismiss()
finish()
}
setCancelable( false )
create()
}
alertDialog.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment