Android Compose on button click go to sleep (turn screen off)

285 views Asked by At

Like title said how to on click button to go system sleep, to turn off screen?

got this kotlin code:

@Composable
fun ButtonOnClickGoToSystemSleep() {
    // Fetching the Local Context
    val localContext = LocalContext.current

    Button(onClick = {
        val intent = Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN")
        intent.putExtra("android.intent.extra.KEY_CONFIRM", true)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        localContext.startActivity(intent)
    }) {
        Text(text = "Go to sleep",Modifier.padding(start = 10.dp))
    }

By the way this is not working, got this from stackoverflow 5 or 10 years ago..

0

There are 0 answers