More than one Superuser command Android

2019-07-02 07:07发布

I'm tring to run this:

            String[] hin1 = { "su", "-c",
                    "mount -o remount,rw -t yaffs2 /dev/block/mtdblk3 /system" };
            try {
                Runtime.getRuntime().exec(hin1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

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

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

Sadly it is only working when I make for every action a new button.. :(

Is there a way to run more than one command at once??

Thanks

3条回答
等我变得足够好
2楼-- · 2019-07-02 07:41

Depending on how the su command is implemented (ie, if it's launching something approaching a capable shell as it would on a more typical linux), you may be able to combine multiple commands into one string by separating them with semicolons.

You could also make a shell script containing multiple commands and use su to launch that, though you may need to put it in an executable location.

查看更多
ら.Afraid
3楼-- · 2019-07-02 07:42

Don't think so its working also , I tried the following code :

public class GainrootActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    }

    public void gainroot(View view)
    {
        String[] hin1 = { "su", "-c","chmod 777 dev/test1" };
        try {
                Runtime.getRuntime().exec(hin1);
            } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

  }
}

only button for command su -c chmod 777 dev/test1 (for changing the permission of one log file in dev directory) but it didn't work. Whats wrong in this.Can some one point out whats missing. I have even put this line in the Androidmanifest.xml as well

<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />

Rgds, Saurabh

查看更多
乱世女痞
4楼-- · 2019-07-02 07:54

You're not letting one command finish before the next one starts. Try adding a waitFor after the exec:

Runtime.getRuntime().exec(hin1).waitFor();
查看更多
登录 后发表回答