3

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!

technor
  • 71
  • 1
  • 4

2 Answers2

1

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.

discuss.kotlinlang.org

Alexei Martianov
  • 2,839
  • 7
  • 28
  • 62
1

Maybe you still can. Rather old post here https://forum.xda-developers.com/showthread.php?t=1210421 says to use gotosleep function. There are discussions of problems with it for Java - see below links, but maybe for Kotlin it will work.

Attempting to put Android device to sleep, but the PowerManager does not contain a "goToSleep(long) method

Android system application DEVICE_POWER permission error

Android What permissions required to call PowerManager.goToSleep(n) put device in sleep mode?