Android--Program to determine the root status of the phone, get root permissions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.