Runtime Permissions

Learn how to integrate permission handling

Android Blaze separates permissions into two categories which fit most scenarios in Android applications: required and optional permissions.

Required permissions are the kind of permissions needed for your app to function. Optional permissions are required for secondary features and the app can still function without them.

 

How to integrate required permissions

Use the PermissionRequired() composable to wrap the screen of your app that requires a specific permission. As soon as the required permission is granted, the wrapped screen will be displayed.

The following sample shows how to require the SEND_SMS permission, before showing the ‘Permission was granted text’:

PermissionRequired(
    permission = Manifest.permission.SEND_SMS,
    title = "SMS permission required",
    description = "We need this in order to send SMS"
) {
    Text("Permission was granted")
}
 

How to ingrate optional permissions

Use the rememberRequestOptionalPermissionLauncher() with the optional permission required. You also need to pass a SnackbarHostState() .

 

When the user declines the permission, a snackbar will be displayed in the SnackbarHost location. The snackbar will include an action to navigate the user to the App system settings so that they can enable the permission manually:

 
val snackbarHostState = remember{ SnackbarHostState() }
val launcher = rememberRequestOptionalPermissionLauncher(android.Manifest.permission.SEND_SMS, snackbarHostState){
   debugLn { "Optional permission granted" }
}

Box(modifier = Modifier
    .fillMaxSize(),
    contentAlignment = Alignment.Center){

Button(onClick ={ launcher.launch() }){
	Text("Request optional permission")
}

SnackbarHost(hostState = snackbarHostState,
        modifier = Modifier.align(Alignment.BottomCenter))
}
 

Related Resources

Did this answer your question?
😞
😐
🤩