How can I wait for SU access after request?
My app requests SU access something like this:
Process p = null;
p = Runtime.getRuntime().exec("su");
That prompts the user to allow or deny the access, but my app continues to execute the next line 开发者_运维技巧of code while the choice is displayed on the screen. How can I make the app wait until the user makes a choice at the prompt?
Just to this, I think it will work:
Process p = Runtime.exec("foo");
int exitCode = p.waitFor();
it will wait for your process to end, and you'll also get the exit code.
Similar to @jdourlens ans you can try this add this code before your actual "su" call.
try
{
Process p2 = Runtime.getRuntime().exec("su -c ls");
int exitCode = p2.waitFor();
}
catch(Exception e2)
{//do something}
So if the user selects "Always allow" option, things will work out. I was also trying to read code Chainfires's library as well his website: https://github.com/Chainfire/libsuperuser/blob/master/libsuperuser/src/eu/chainfire/libsuperuser/Shell.java
What I could figure out was to do a Blocking I/O call while the user grants super user access. A good way is to write a temp file, or do some random commands. This is what I tried to do. Call ls with su, which will grants that process su access and then your next su call will have the appropriate access.
精彩评论