Search code examples
kotlinandroid-jetpack-composeandroid-permissions

Why is the permission window not showing in jetpack compose?


I have been working with AI to get a permission window to show, but nothing shows up when i press on the button. This is the code we came up with


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var permissionGranted = false

        setContent {
            PermissionsTheme {
                // A surface container using the 'background' color from the theme
                val permissionLauncher = rememberLauncherForActivityResult(
                    contract = ActivityResultContracts.RequestPermission() // Specify single permission request
                ) { result ->
                    permissionGranted = result
                    // Update UI based on permission result directly within the lambda
                }
                Button(onClick = {
                    permissionLauncher.launch(Manifest.permission.INTERNET)
                }) {
                    Text(text = "Request Internet Permission")
                }

                // UI will update immediately after permission dialog closes
                if (permissionGranted) {
                    Text(text = "Allowed")
                } else {
                    Text(text = "Denied")
                }
            }
        }
    }
}

and the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

    <application

I have built a lot of little apps that uses permissions and I always have a problem with them. I've been following tutorials on permissions, I've read the android developers guide on permissions. I took a deep dive one each and every statement of the permission process with AI, I asked AI to write me a basic permission app, but I just can't get it to work. What am I doing wrong?


Solution

  • android.permission.INTERNET is an install-time permission and not a runtime permission. This is documented on the Connect to the network documentation on developer.android.

    I found a pretty concise tutorial on requesting permissions using Jetpack Compose here that would do a better job explaining the process.