Execute multiple root shell commands in Android -
Execute multiple root shell commands in Android -
i'm developing application execute multiple shell command in different time. i'm using next method:
public void runasroot(string[] cmds){ process p = runtime.getruntime().exec("su"); dataoutputstream os = new dataoutputstream(p.getoutputstream()); (string tmpcmd : cmds) { os.writebytes(tmpcmd+"\n"); } os.writebytes("exit\n"); os.flush(); }
this method works fine open new shell every time need phone call it, display annoying toast "application has been granted root permission". think becouse open , close new shell su access. question is: there way leave su shell open can run commands when needed without receive su toast?
so might bit late, in case still searching solution: declare
private process p = runtime.getruntime().exec("su");
globally in class. should fit needs. can spawn process in onresume()
, destroy()
1 time again in onpause()
.
@override onresume() { if(//check root) { seek { this.p = runtime.getruntime().exec("su"); } catch(ioexception e) { // exception handling goes here } } //set else } @override onpause() { this.p.destroy(); }
btw: see serious memory leak in above method: open variety of su processes never destroy()
them again. depending on how phone call method there more , more su processes lying around in ram until app closed.
so
public void runasroot(string[] cmds){ seek { process p = runtime.getruntime().exec("su"); } catch(ioexception e) { // exception handling goes here } dataoutputstream os = new dataoutputstream(p.getoutputstream()); (string tmpcmd : cmds) { os.writebytes(tmpcmd+"\n"); } os.writebytes("exit\n"); os.flush(); p.destroy(); }
would here. i'm asking myself if can compile approach. runtime.getruntime().exec();
has surrounded try/catch.
android root
Comments
Post a Comment