Reputation: 541
I have very simple code as follows:
@Override
public void onClick(View v)
{
Log.i("MyApp", "Started");
try
{
Process processStart = Runtime.getRuntime().exec("su");
}
catch (IOException e1)
{
e1.printStackTrace();
}
String myStringArray[]= {"getevent","/dev/input/event0"};
String line;
try
{
Process process = Runtime.getRuntime().exec(myStringArray);
InputStreamReader inputstreamreader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputstreamreader);
bufferedReader.read();
while ((line = bufferedReader.readLine()) != null)
{
Log.i("MyApp", line);
}
InputStreamReader errstreamreader = new InputStreamReader(process.getErrorStream());
BufferedReader errReader = new BufferedReader(errstreamreader);
errReader.read();
while ((line = errReader.readLine()) != null)
{
Log.i("MyApp", line);
}
} catch(java.io.IOException e){
}
Log.i("MyApp", "Finished");
}
});
but I get this error:
could not open /dev/input/event0, permission denied
I get asked to grant root permission on the phone but before that it have error on logcat.
I have also tried with processStart.waitFor();
But it hangs the application there and does not move forward at all. I have tried looking the reason everywhere but could not get this to work.
I also tried with ProcessBuilder but when I use that the getevent returns nothing at all...
Upvotes: 3
Views: 5570
Reputation: 10368
I think you have misunderstood the usage of 'su'.
Process processStart = Runtime.getRuntime().exec("su");
The above code will create a new process and execute the 'su' command, which will only make the child process to become 'root'.
Your calling process is still a normal process so that you cannot do 'getevent'. Instead you should run this super command in your 'rooted child process' like:
mProcess = new ProcessBuilder()
.command("/system/xbin/su")
.redirectErrorStream(true).start();
OutputStream out = mProcess.getOutputStream();
String cmd = "getevent /dev/input/event0 \n";
Log.d(TAG, "Native command = " + cmd);
out.write(cmd.getBytes());
Upvotes: 6