1 Public Booleangetrootpermission (String pkgcodepath) {2LOG.D (TAG, "pkgcodepath=" +Pkgcodepath);3Process Process =NULL;4DataOutputStream OS =NULL;5 Try {6String cmd = "chmod 777" +Pkgcodepath;7Process = Runtime.getruntime (). EXEC ("su");//switch to root account8OS =NewDataOutputStream (Process.getoutputstream ());9Os.writebytes (cmd + "\ n"));TenOs.writebytes ("exit\n"); One Os.flush (); A process.waitfor (); -}Catch(Exception e) { -Toast.maketext ( This, "root error!" +Pkgcodepath, Toast.length_short). Show (); the e.printstacktrace (); - return false; -}finally { - Try { + if(OS! =NULL) { - os.close (); + } A Process.destroy (); at - -}Catch(Exception e) { - } - } -Toast.maketext ( This, "root success!" +Pkgcodepath, Toast.length_short). Show (); in return true; -}getrootpermission
Then execute the method where you want to apply for root.
Getrootpermission (Getpackagecodepath ());
Silent Installation
Http://www.2cto.com/kf/201503/381170.html
http://ju.outofmemory.cn/entry/108796
Request Root Permission