Jetpack Compose
Runtime Permissions In Jetpack Compose
How to properly handle android runtime permissions in jetpack compose
Asking your users for necessary permissions is an important part of developing any Android app. In this article, we will explore how to handle requesting runtime permissions in an Android app using Jetpack Compose. We will create a user-friendly approach to requesting permissions and handling the user interactions as you can see in the video bellow.
Add Permissions to the Manifest File
The android manifest file describes the essential information about your app to the Android build tools, the Android operating system, and Google Play. Adding permissions to the manifest file is necessary, this is how the android operating system knows that you will have access to such a permission.
In this example we’re requesting these permissions, that all need to be granted by the user:
- ACCESS_COARSE_LOCATION : this permission gives the app access to the approximate location of the device.
- READ_CALENDAR: this permission gives the app access to read the user’s calendar events and information.
- READ_CONTACTS: this permission gives the app access to read the user’s contacts.
- RECORD_AUDIO: this permission gives the app access to use the microphone.
Define the Permissions Enum
In the permissions enum you will define the needed permissions for your app. By defining an enum, you can centralize the definition and the management of the necessary permissions and their associated properties such as the title and description, making it easier to reference them throughout your app.
In this example we’re defining an enum for the four permissions that we added to the manifest file, with these properties:
- permission: this is the appropriate permission name from the android package.
- title: a title for the requested permission.
- description: a brief description for the user about why you need this permission.
- permanentlyDeniedDescription: this description is shown when the user permanently denied granting the permission, this means that the permission need to be granted from the app settings.
- the function permissionTextProvider(isPermanentDenied: Boolean): will return the appropriate description based on the permission status (denied or permanently denied).
- and the last function getNeededPermission(permission: String): will take the permission name as it’s in the android package and return a permission enum, we will see it use later.
Create the Alert Dialog
When the user refuses to grant a permission, the alert dialog will be shown to provide a clear explanation to the user of why the permission is needed and how it will be used.
This alert dialog composable function will take these properties as a parameter:
- neededPermission: this is the needed permission enum that we created before.
- isPermissionDeclined: this will be true when the user permanently declined the permission and it need to be granted in the app setting.
- onDismiss: this lambda function will be called when the user dismiss the dialog.
- onOkClick: this will be called when the user click the “ok “ button to grant the permission.
- onGoToAppSettingsClick: this will be called when the user click the “go to app settings” to go to the app settings and grant the permission.
Create the Permissions Composable
Now we will create a simple UI of two buttons one for requesting one permission (we’re requesting the microphone as an example), and the other one for requesting multiple permissions.
- The PermissionDialog variable is a mutable state list that keeps track of the permissions that have been declined by the user and need to be shown in alert dialog.
- The microphonePermissionLauncher and multiplePermissionLauncher variables use the rememberLauncherForActivityResult function to launch permission requests and handle the results. The onResult callback is used to add declined permissions to the PermissionDialog list.
- The goToAppSetting function is an extension function that launches the app settings screen to allow the user to grant a declined permission manually.
Call the Permissions Composable in the Main Activity
And finally we will simply call the permissions composable in the main activity.
Conclusion
In conclusion, the code provided in this article demonstrates how to handle requesting runtime permissions in Jetpack Compose, while providing a good user-friendly way to request permissions and handle user interactions. The full code can be found in my GitHub repository bellow.