Sometimes I run a server app on my Android device (OnePlus 6 rooted with Android 10) but it appears that most apps only run when the screen is on which is a much worse battery drainer than background services. I know that scrcpy which is an adb powered app which allows the control the device from a computer over USB or Wifi and the 'virtual screen' (i.e. display in the scrcpy app on the computer) is on but the physical screen is off. And this app does not use root, so it should be possible. How do they do that ?
3 Answers
scrcpy --stay-awake --turn-screen-off >& /dev/null & exit
-
1Please read How do I write a good answer?. While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. Commented Jul 27, 2022 at 10:53
scrcpy author explained it here:
To turn the screen off, scrcpy calls the private API
setDisplayPowerMode()
by introspection here.To execute such Java code from shell:
- https://blog.rom1v.com/2018/03/introducing-scrcpy/#run-a-java-main-on-android
- https://github.com/Genymobile/scrcpy/blob/master/DEVELOP.md#privileges
AFAIK there is command to turn the screen off via
adb
only.
To wake up my Galaxy I start scrcpy
from my mac with this command:
scrcpy --tcpip="[DEVICE IP]" --stay-awake
DEVICE IP is the IP of the Android device.
Whenever it goes black I type in the Android PIN (numbers and then ENTER on my laptop) to unlock the device.