1

i'm trying to execute a command with root privileges, i try this(only for example):

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

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
    result += line;
    Log.d(TAG,"RESULT:"+result);
}

But i need to call it in a cicle with hundred times, so this way is very slow and always is showing a dialog with the message of the root privileges are granted, so how i can create a sigle su process for write and read consecutively using input and output streams? i know to use both but i dont know how to write and the read the result of the command. Thanks for your help.

7
  • you expect 'whoami' to return something different after the first time? Commented Jan 4, 2013 at 22:15
  • no, whoami is only for example, realy i want to execute a .sh file by command line, this file can do what ever i want and it return a string, the center of the problem is executing any command any number of times but with root privileges without call su by each one, my english is not good so i guess if i explained me well, and thanks. Commented Jan 4, 2013 at 22:47
  • I'd suggest writing another shell script that executes what you want in a loop, and calling that instead of the raw command. Commented Jan 4, 2013 at 22:49
  • thanks, but i'm using a progress bar for track the progress, is not way to write and read in the Process through streams? Commented Jan 4, 2013 at 23:14
  • Yes, your sample code is already doing that, through the BufferedReader 'reader' Commented Jan 4, 2013 at 23:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.