I want to get permission using Jetpack Composer on Android, but I can't see the confirmation screen.

Asked 1 years ago, Updated 1 years ago, 408 views

I would like to make a permission request using Jetpack Composer and Accompanist on Android in a Jetpack-like way.

https://google.github.io/accompanist/permissions/

I'd like to get permission for Foreground Service, but when I call launchPermissionRequest, I don't see a dialog box asking permission.
It seems that isGranted is returning true even though I don't remember permitting permission.

Why?

This phenomenon occurs when you run a debug-built application on a real machine.
Are all permissions allowed by default in debug builds?

The behavior of pressing the button depends on whether servicePermissionState.status.isGranted is true with the following code:
AndroidManifest also mentioned this permission.

Assume Android 12.

fun BigButton(){
    val servicePermissionState=rememberPermissionState(permission=android.Manifest.permission.FOREGROUND_SERVICE)
    val listener = MyListener()
    varis_enable=remember {mutableStateOf(true)}
    value text = if(servicePermissionState.status.isGranted) "Enable" else "Disable"
    var color=if(is_enable.value)Color.DarkGray elseColor.Cyan
    val context = LocalContext.current
    if(!servicePermissionState.status.isGranted){
        color=Color.Red
    }
    Button(modifier=Modifier)
        .size (width=220.dp, height=80.dp),
        colors=ButtonDefaults.buttonColors (backgroundColor=color), onClick={
            if(servicePermissionState.status.isGranted){
                is_enable.value=!is_enable.value
                Log.i("Granted", servicePermissionState.status.isGranted.toString())

            } else{
                Log.i("False", "false")
                servicePermissionState.launchPermissionRequest()
            }
        }) {
        Text(text)
    }
}

android kotlin jetpack-compose

2022-12-15 04:40

1 Answers

From Android 9, FOREGROUND_SERVICE is automatically granted.

If use-permission is defined in manifest, it is auto-grant.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

https://developer.android.com/about/versions/pie/android-9.0-migration#tya


2022-12-15 05:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.