EzeBongiovi
EzeBongiovi

Reputation: 63

Need to execute multiple commands on Runtime.getRunTime().exec

public void uninstallApp(String packageName){
       try {

                String[] command = new String[4];
                command[0] = "su";
                command[1] = "mount -o remount,rw /system";
                command[2] = "rm " + packageName;
                command[3] = "reboot";
                Runtime run = Runtime.getRuntime();
                run.exec(command);

                Log.d("DELETED", packageName);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e("ERROR ON DELETE", e.getMessage());
    }
}

That's my code. This method receives an apk's path, what i need to do is execute those commands but without loosing permissions. I mean, if i execute "su" in one process and then "mount" in other process. "mount" won't have "su" access. So i need to execute all those commands in a single process. And that's not working. :\

Upvotes: 1

Views: 5332

Answers (2)

Emad Razavi
Emad Razavi

Reputation: 2113

You don't need to use ProcessBuilder pattern. just add your commands to the String array in order:

Process process = Runtime.getRuntime().exec(new String[]{"su", "-c", command1, command2});

Upvotes: 0

EzeBongiovi
EzeBongiovi

Reputation: 63

FOUND SOLUTION

StringBuilder cmdReturn = new StringBuilder();

try {
    ProcessBuilder processBuilder = new ProcessBuilder("su","-c mount -o remount,rw /system ; rm " + packageName + " ; reboot");
    Process process = processBuilder.start();

    InputStream inputStream = process.getInputStream();
    int c;
    while ((c = inputStream.read()) != -1) {
        cmdReturn.append((char) c);
    }

    Log.d("CMD RESPONSE", cmdReturn.toString());

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Upvotes: 1

Related Questions