3

I'm building app on Raspberry Pi with Android Things and I have 7 inch touch screen, but the screen never turns off.

Is it possible to set timeout like in Android phones? or force it to turn off/on.

If I remove the power line and inserted again the screen, it will not work until I reboot.

Onik
  • 19,396
  • 14
  • 68
  • 91

2 Answers2

4

There are two system settings that control this process: STAY_ON_WHILE_PLUGGED_IN and SCREEN_OFF_TIMEOUT

The STAY_ON_WHILE_PLUGGED_IN setting is set to BATTERY_PLUGGED_AC by default. You can programmatically change this value in your application code with the Settings API:

Settings.Global.putInt(getContentResolver(),
        Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);

You can also make this change from the command line if it is more convenient:

$ adb shell settings put global stay_on_while_plugged_in 0
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Which method did you use? I just realized there was an error in the shell command. – devunwired Jul 20 '17 at 14:06
  • The screen is still on, he never go to sleep – Magni Þór Birgisson Aug 03 '17 at 16:48
  • The first suggestion to use STAY_ON_WHILE_PLUGGED_IN throws a permission error: java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS. However you can't add the permissions to the manifest as permission is only granted for system apps. – ThanksMister Aug 29 '17 at 16:35
  • Using SCREEN_OFF_TIMEOUT requires android.permission.WRITE_SETTING but this is also never granted. You can check if you have permission using Settings.System.canWrite(this) which always returns false. Using "adb shell settings put global stay_on_while_plugged_in 0" seems to freeze the display. – ThanksMister Aug 29 '17 at 16:52
2

The approach below works starting with Android Things Developer Preview 5.0. For older releases it has no effect.

Turn off display:

adb shell settings put global stay_on_while_plugged_in 0

Turn on display:

adb shell settings put global stay_on_while_plugged_in 1
adb shell input keyevent 26

For a programmatic solution see @Devunwired's answer.

Onik
  • 19,396
  • 14
  • 68
  • 91
  • Now it is black but the led-backlit is on do you know how to turn it complete off. It will not wake up if I touch the screen. If I insert usb keyboard it will wake up – Magni Þór Birgisson Sep 12 '17 at 21:59