Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on and turn off screen with adb command? [duplicate]

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();
like image 250
John Avatar asked Dec 26 '16 06:12

John


People also ask

How do I turn on my phone with adb?

If you enabled USB debugging and installed Minimal ADB & Fastboot before the phone turned off, go to your computer and launch the tool. Plug in your phone via USB, then type adb reboot and hit enter. Also try adb reboot recovery. This will not work for all phones when they are switched off, but it is worth a try.

How can I shutdown my Android phone using an adb command?

Type “adb shell reboot -p” and press enter. For Terminal, type “. \adb shell reboot -p” and press Enter.

How do I run adb from command line?

Open a command window in the folder by holding shift and right-clicking in an empty spot in the folder and selecting "Open command prompt/PowerShell here" in the menu. Then you can start using ADB — connect your phone and try . ADB devices to see if it's working. A list with attached devices should show up.


2 Answers

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

like image 78
Vova Avatar answered Oct 06 '22 00:10

Vova


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
like image 39
0xAliHn Avatar answered Oct 05 '22 23:10

0xAliHn