开发者

How to run multiple shell commands through an app in android

I am trying to run 3 different commands through my app but only the first one is getting executed. here's the code.

开发者_C百科Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("mount -o remount,rw /system");
process = Runtime.getRuntime().exec("cp /sdcard/hosts /system/etc");

I get the root access but after that nothing else happens.

EDIT: I tried this code but this also executes only su comand

String[] commands = {"mount -o remount,rw /system", "cp /sdcard/hosts /system/etc"};

                        Process p = Runtime.getRuntime().exec("su");
                        DataOutputStream os = new DataOutputStream(p.getOutputStream());            
                        for (String tmpCmd : commands) {
                                os.writeBytes(tmpCmd+"\n");
                        }           
                        os.writeBytes("exit\n");  
                        os.flush();

EDIT: This works but only one command at time, i'll have to make a button for every command.

String[] hin1 = { "su", "-c","cp /sdcard/Mediafire/hosts /system/etc/" };
                     try {
                         Runtime.getRuntime().exec(hin1);
                     } catch (IOException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }


This is easy to do.

Use "root tools."

Add the jar file from this link: https://github.com/Stericson/RootTools.

Command command = new Command(0, "echo this is a command", "echo this is another command"){
        @Override
        public void output(int id, String line)
        {
            //Do something with the output here
        }
};
RootTools.getShell(true).add(command).waitForFinish();


Root is not "sticky" because the su hack is neither intended (nor on a unix-like operating system) able, to change the user ID of an existing process.

Some versions of su will let you specify a command and its parameters to be executed. But others will not, and instead require that you open the input stream of the superuser shell created by the su program, and push commands into that as if you were typing them.

Rather than provide an example, I'm suggesting the question be closed as a duplicate of one where code is provided.


Exec runs a command in a separate process, so I would expect the effects of "su" to be lost once that process finishes. So maybe the mount then fails as it's in a separate process, where su has not been applied.

Can you put your sequence of commands in a single file to be executed?

Alternatively can you use su -c to get the job done in one command?


easy with a &

Process process = Runtime.getRuntime().exec("su & mount -o remount,rw /system");


sorry for late answer. i am able to run multiple commands through my app. and i am sure you also able to run multiple commands on root device.

here is my code :

  try {
                    //below code for getting root access.
                Process processcmd = Runtime.getRuntime().exec("su");
                    OutputStream os = processcmd.getOutputStream();
                    //below code for capture screenshot and placed in internal storage.
                    os.write("screencap sdcard/my.png\n".getBytes());
                    // below code for execute shell commands
                    os.write("adb shell\n".getBytes());
                    //below code for increase brightness.
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());
                    os.write("input keyevent 221\n".getBytes());

                    //below code for decreasing brightness.
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());
                    os.write("input keyevent 220\n".getBytes());

                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }

now in above code i first check root access then take screenshot of current screen focus and placed it in internal storage then execute adb shell for executing extra command then i increase brightness.

NOTE: You must put new line character "\n" at ending of each command .

.

hope this also helpful to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜