Determine if the phone has root privileges
/*** Determine if the phone is rooted*/ Public BooleanIsRoot () {BooleanRoot =false; Try { if((!NewFile ("/system/bin/su"). Exists ())&& (!NewFile ("/system/xbin/su"). Exists ())) {root=false; } Else{root=true; } } Catch(Exception e) {}returnRoot; }
The parameters returned above will tell if the phone has root privileges.
Send the request message to root to get root privileges
This method does not involve the underlying, which requires the user to click on the confirmation to obtain.
Public classMainactivityextendsActivity {/*** Create*/ Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //returns the System Package nameString apkroot = "chmod 777" +Getpackagecodepath (); Rootcommand (Apkroot); } /*** Application Run command to get root permission, device must be cracked (get root permission) * *@paramCommand * Commands: String apkroot= "chmod 777" +getpackagecodepath (); * Rootcommand (Apkroot); * @returnApplication Yes/No get root permissions*/ Public Static Booleanrootcommand (String command) {process Process=NULL; DataOutputStream OS=NULL; Try{Process= Runtime.getruntime (). EXEC ("su"); OS=NewDataOutputStream (Process.getoutputstream ()); Os.writebytes (Command+ "\ n"); Os.writebytes ("Exit\n"); Os.flush (); Process.waitfor (); } Catch(Exception e) {log.d ("* * * DEBUG", "ROOT REE" +e.getmessage ()); return false; } finally { Try { if(OS! =NULL) {os.close (); } Process.destroy (); } Catch(Exception e) {}} LOG.D ("* * * DEBUG", "Root SUC"); return true; } }
I'm the dividing line of the king of the Land Tiger.
Reference: http://blog.csdn.net/fm9333/article/details/12752415