18

I am using KEYCODE_POWER to turn on and turn off my rooted phone. The bellow command is used in both case turn on and turn off the screen.

adb shell input keyevent KEYCODE_POWER

However, I want to use it in separated cases: turn on and turn off. I have two functions: turn on and turn off functions. If the screen is off and I call the turn on function, it will turn on the screen. if the screen is already turn on, the turn on function will not do anything. Otherwise, If the screen is on, I will call turn off function and it will turn off.

I tried to check the screen state but it does not work well. Actually, the screen state update is so slow comparison with processing of phone. I also use other way but these ways make the screen wakeup without sleep.

final Window win = getWindow();
    win.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON ); 

Second way:

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wl.acquire();
John
  • 2,838
  • 7
  • 36
  • 65

2 Answers2

27
adb shell input keyevent 26

26 - is the keyevent code power button on the device.

You can find more command by link : http://adbshell.com/commands

Mercury
  • 7,430
  • 3
  • 42
  • 54
Vova
  • 956
  • 8
  • 22
  • 2
    It also is off .... – John Dec 26 '16 at 08:04
  • Can you send me command response. – Vova Dec 26 '16 at 08:06
  • 26 is power action. You can think what is happen when screen is on and you press power button. – John Dec 26 '16 at 08:09
  • if I understand correctly your question. You want have listener on the display with 'turn on' and 'turn off' callbacks. Did I understand you? @us – Vova Dec 26 '16 at 08:29
  • @VovaStelmashchuk 4 years later reply :) Here's the full list - https://developer.zebra.com/community/home/blog/2015/03/09/introduction-to-adb-shell-commands **or** - https://stackoverflow.com/a/8483797/4206951 – Oush Jun 14 '20 at 15:46
7

You can write a script to control turn ON/OFF of the screen. Here is the sample script code:

result="$(adb shell dumpsys input_method | grep -c "mScreenOn=true")"

if [ "$result" == 1 ]; then
    echo "Screen is already on."

else
    echo "Turning screen on."
    adb shell input keyevent 26
fi
0xAliHn
  • 18,390
  • 23
  • 91
  • 111
  • 1
    mInteractive worked better for me - mScreenOn did not appear on much devices for me – ligi Dec 15 '19 at 07:22
  • 1
    for android 10 always value = 0 – Sidara KEO Apr 16 '20 at 09:18
  • 1
    Here's a one-line version: `(dumpsys power | grep mWakefulness=Awake > /dev/null) && input keyevent 26` – Alexander Pruss Jan 18 '22 at 15:12
  • 1
    There is no "mScreenOn" on the few devices I checked (android 12 and 13) But there is screenOn :: "screenOn = false" or "screenOn = true" "dumpsys power | grep mWakefulness" is good. I use "getprop debug.tracing.screen_state" for now. "2" means screen ON. – rejdrouin Feb 19 '23 at 00:08