I use KEYCODE_POWER to turn my root phone on and off. The bellow command is used in both cases and turns off the screen.
adb shell input keyevent KEYCODE_POWER
However, I want to use it in some cases: turn it on and off. I have two functions: turning functions on and off. If the screen is off and I call the enable function, it will turn on the screen. if the screen is already on, the enable function will do nothing. Otherwise, if the screen is on, I will call the shutdown function and it will turn off.
I tried to check the status of the screen, but it does not work. In fact, updating the state of the screen is so slow compared to processing the phone. I also use another method, but these methods make the screen wake up 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 );
The second way:
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wl.acquire();