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
  • 5

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

Alex Martian
  • 3,423
  • 7
  • 36
  • 71
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?

Child Detektiv
  • 237
  • 1
  • 12
  • maybe, but from links you provided looks that option is possible for API<21, that is for devices running Android 4 and below. – Alex Martian Sep 11 '19 at 10:54