Hello!
How to send Android device to sleep or programmatically press the power button(KEYCODE_POWER) with Kotlin?
Is it possible?
Some example?
Thank you advance!
Hello!
How to send Android device to sleep or programmatically press the power button(KEYCODE_POWER) with Kotlin?
Is it possible?
Some example?
Thank you advance!
This is not a Kotlin question. Anything that can be done in Java on Android can be done with Kotlin.
In Android (regardless of whether you use Kotlin, Java, whatever) for security reasons it is not possible for an app to make the device sleep, unless your app is a system app provided by the phone manufacturer.
The only options if you really want the device to sleep are to partner with a phone manufacturer to have your app preloaded as a system app (so it’s a business development issue rather than a technical issue), or if it’s just for personal use, root your phone to allow your app to run as a system app. In either case, you can then use PowerManager.goToSleep().
If you just want to turn off the screen temporarily while your app is running, just get a wake lock and set the screen brightness in the wake lock to the minimum value. The screen will still be on, but on most devices it will be really really dim for as long as your app is running.