Is it possible to execute ADB shell commands from app?

牧云@^-^@ 提交于 2019-12-10 10:17:59

问题


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



来源:https://stackoverflow.com/questions/28785411/is-it-possible-to-execute-adb-shell-commands-from-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!