简体   繁体   中英

How do you run terminal commands in a created android application on rooted emulator?

I'm currently creating an android application that will run some terminal commands. I am using a genymotion Nexus 5 (4.4.4) emulator that comes with root by default. However, to test some general commands I did the following:

Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec(new String[]{"mkdir /sdcard/tmp1/"});
process = Runtime.getRuntime().exec(new String[]{"adb pull /sdcard/tmp3/new4.txt \"C:\\Users\\my_username\\Documents\\\""

When the "su" executes i get a message from an app named SuperUser saying I have have been given root privileges. However none of the other commands are executed. No tmp1 direcotry is made and I can't pull the new4.txt file, which I manually added to the emulator, to my documents folder.

Am I missing something in my path strings?

When the "su" executes i get a message from an app named SuperUser saying I have have been given root privileges

So I guess you are running the code on the phone. In this case you are also running the third command ( adb pull ) on the phone, which you want to run on the host instead.

As this answer points out you are running consecutive commands in different sub-shells. So your first su will not matter in the second exec 's context. To execute a command with su see the manual page of su . A simple exeample is:

su - -c "whoami"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM