This is a sample Android app that demonstrates how to request camera and microphone permissions using Jetpack Compose.
- Requests camera and microphone permissions.
- Shows a dialog when permissions are denied.
- Navigates to app settings when permissions are permanently denied.
- Clone the repository:
git clone https://github.com/Bhavyansh03-tech/Permissions_Handling.git
- Open the project in Android Studio.
- Build and run the app on your Android device or emulator.
If the user denies the permission twice, a dialog is shown to explain why the permissions are needed.
permissions.forEach { permission ->
val isGranted =
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
// If Permission is not granted :->
if (!isGranted) {
if (shouldShowRequestPermissionRationale(permission)) {
// If user decline permission 2 times which case should show when request permission.
// Directly show dialog :->
mainViewModel.updateShowDialog(true)
} else {
// Ask for permission :->
permissionsResultActivityLauncher.launch(permissions)
}
}
}
The app displays a dialog to inform the user about the need for permissions and provides an option to go to the app settings if the permissions were permanently denied.
if (showDialog) {
PermissionDialog(
onDismiss = {
mainViewModel.updateShowDialog(false)
},
onConfirm = {
mainViewModel.updateShowDialog(false)
if (launchAppSettings){
// In case you want to ask permission through app settings :->
Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", packageName, null)
).also {
startActivity(it)
}
mainViewModel.updateLaunchAppSettings(false)
} else {
// In case you want to ask permission through dialog :->
permissionsResultActivityLauncher.launch(permissions)
}
}
)
}
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Create a new Pull Request.
For questions or feedback, please contact @Bhavyansh03-tech.