See complete example below for single permission request
@Composable
funGreeting() {
val context =LocalContext.current
val scope = rememberCoroutineScope()
var permissionStatus by remember { // Track status of permission
mutableStateOf(Status.INITIAL)
}
val request = requestPermission(permission = android.Manifest.permission.CAMERA,
onChangedStatus = { permissionStatus = it})
Column(modifier =Modifier
.fillMaxSize(), horizontalAlignment =Alignment.CenterHorizontally,
verticalArrangement =Arrangement.Center) {
//you can use if(permissionStatus) if you don't interest all states when(permissionStatus){
Status.GRANTED_ALREADY-> {//(No need to request permission) or (permission requested and granted already)Text(text ="Permission Has already Granted")
}
Status.NOT_ASKED-> {//a place to request permissionText(text ="No permission request has made")
}
Status.DENIED_WITH_RATIONALE-> {
Text(text ="Permission has denied once but you have still have a chance to show permission popup")
}
Status.DENIED_WITH_NEVER_ASK-> {//call context.openAppSystemSettings() to navigate user to app settingsText(text ="Permission has denied navigate user to app settings")
}
else-> {}//Nothing
}
Button(onClick = {
//Request permission
request.launch(android.Manifest.permission.CAMERA)
scope.launch {
delay(1000)
if (permissionStatus ==Status.DENIED_WITH_NEVER_ASK&& context.activity()?.hasWindowFocus() ==true){ //See below for why hasWindowFocus should be true
context.openAppSystemSettings()
}
}
}) { Text("Request permission") }
}
}
Multiple Permissions Request
Track status of permissions with permissionStatus
var permissionStatus by remember {
mutableStateOf(mapOf<String,Status>())
}
val request = requestMultiplePermission(
permissions =listOf(
Manifest.permission.CAMERA, Manifest.permission.POST_NOTIFICATIONS
), onChangedStatus = { permissionStatus = it}
)
If all permissions have granted, permissionStatus.allGranted() will be true
If all permissions have not granted, permissionStatus.allDenied() will be true
funMap<String,Status>.allGranted():Boolean{
returnthis.values.all { it ==Status.GRANTED_ALREADY }
}
funMap<String,Status>.allDenied():Boolean{
returnthis.values.all { it !=Status.GRANTED_ALREADY }
}
It is not possible to observe changes, if user has manually changed permission in app settings
If user has denied permission with never_ask and changed permission manually, it will be better to request permission and check any window popuped or not.