2

I'm working in an app that needs to to do any of this actions:

  • Lock the device
  • Put the device in sleep mode
  • Turn off the screen

How can I achieve this?

Found this option, but it requires the proximity sensor to be in "near" state to turn off the screen:

 mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
 mWakeLock.acquire();

Thanks

Addev
  • 31,819
  • 51
  • 183
  • 302
  • I highly doubt Android lets you do this. In any case, you could try your luck with [Android: How to turn screen on and off programmatically?](https://stackoverflow.com/a/41968655/962089) if the conditions are acceptable and it works on supported devices/OS versions – chris Mar 12 '18 at 18:58

1 Answers1

3

If your app is a device admin you can lock the screen with DevicePolicyManager.lockNow()

You can use this snippet (after setting the Device Admin part):

DevicePolicyManager manager = ((DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE));
manager.lockNow();
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
0x7067
  • 86
  • 6