Is it possible to execute ADB shell commands from

2019-05-11 00:44发布

问题:

I have an Android PC with root (out-of-box) connected to an external monitor (HDMI & USB) that always displays in landscape, even though my app specifies portrait in the activity declaration in Manifest:

android:screenOrientation="portrait"

I am trying to execute the following command from this SO post to force portrait:

proc = Runtime.getRuntime().exec("adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0");          
int exitVal = proc.waitFor();

This should set the screen to portrait, but the display always remains in landscape. No exceptions are recorded.

I have also used the following without success:

process = Runtime.getRuntime().exec("content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0");

process = Runtime.getRuntime().exec(new String[]{"su","-c","content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0"});

In both cases, the exit value is [0]. Which means the command terminated normally.

There is only one USB port, and one micro USB port (Power only/no data) on the Android PC. With debugging enabled, I cannot enumerate the device in DDMS, nor execute shell commands from BASH connecting my dev PC to the micro USB port on the Android PC. So, I am trying to do this from my Android app.

Please advise

回答1:

When executing a command on the device there is no need to prefix it with adb shell, your java code runs on the device itself so just drop it off and just run the following command

content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0"

Just a quick note, when you do so, you tell the Android device to execute this command on another android device that is connected to it via ADB