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();
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.
Type “adb shell reboot -p” and press enter. For Terminal, type “. \adb shell reboot -p” and press Enter.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With