I need to get permission to turn do not disturb mode on or off. Normally, without composing I would use the following code and check the result of the launched activity:
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!mNotificationManager.isNotificationPolicyAccessGranted) {
val intentNotifica = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
startActivity(intentNotifica)
}
But with jetpack compose I would not know how to check if the user has obtained the permissions or not. For example, with the following code, I cannot know, after the user has pressed the button, if the permissions have been obtained or not:
fun DoNotDisturbPermission() {
val context = LocalContext.current
val hasPermission = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (!hasPermission.isNotificationPolicyAccessGranted) {
Button(onClick = {
val intentNotifica = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
context.startActivity(intentNotifica)
}) {
Text("get permission")
}
} else {
Text("Already granted")
}}
Also, I tried to use the Accompanist library with rememberPermissionState(Manifest.permission.ACCESS_NOTIFICATION_POLICY)
, but it doesn't work properly.