I'm developing a small application that has to read the contents of a protected folder, so I came to the conclusion that I had to use su in combination with ls and an inputstream.
However, when the readLine function reaches the end of the output, it just hangs. I'm guessing this is because the last line doesn't include an EOL mark...
Code:
p = Runtime.getRuntime().exec("su");
outputS = new DataOutputStream(p.getOutputStream());
inputS = new DataInputStream(p.getInputStream());
errorS = new DataInputStream(p.getErrorStream());
outputS.writeBytes("ls /datadata" + "\n");
outputS.flush();
while((s = inputS.readLine()) != null)
{
Log.i("DS", s);
}
Log.i("DS", "done");
How can I get around this? I've tried reading chars and then just wait for -1, but that didn't work, same thing as above. Ideally I'd do this without system calls, but Android sadly won't let me list or copy the contents of this folder...
Thank you for reading this, Quint
EDIT: Thanks to the answer below I solved my problem, whatever you do, just make sure that you end the "su" task just like you would do when using adb shell. You can use the method described in the accepted answer, or manually run "exit" after you did whatever you needed to do. Running exit does generate two empty lines in the errorstream for reasons unknown to me.