I'm trying to execute a shell command in an android app (Java) and read the output. I can read the output of most commands but I am unable to get anything when I use a pipe in the command. The device is rooted.
I've found this but it doesn't want to work on Android. I've tried output redirects, e.g. "2>&1" at the end of the command but no luck.
Thanks in advance!
Working Command
pm list packages
Not Working Command
pm list packages | grep com.myapp
Code:
public String executeCommand(String command) {
String output = "";
InputStream inputStream = Runtime.getRuntime().exec(command).getInputStream();
while( inputStream.available() <= 0)
try { Thread.sleep(500); } catch(Exception ex) {}
java.util.Scanner s = new java.util.Scanner(inputStream);
while(s.hasNext())
output += s.nextLine() + "\n";
return output;
}
Try and catch blocks omitted for brevity