Android determines if the phone is rooted

Source: Internet
Author: User

A way to determine if a phone is rooted. If the app has some special features that require root access, you'll need to determine if it's root. For example, some markets automatically install after downloading the app.

/** * @author Kevin Kowalewski * */public class Root {private static String Log_tag = Root.class.getName ();        public Boolean isdevicerooted () {if (CheckRootMethod1 ()) {return true;}        if (CHECKROOTMETHOD2 ()) {return true;}        if (CheckRootMethod3 ()) {return true;}    return false;        } public boolean checkRootMethod1 () {String buildtags = Android.os.Build.TAGS;        if (buildtags! = null && buildtags.contains ("Test-keys")) {return true;    } return false;            public Boolean checkRootMethod2 () {try {File file = new file ("/system/app/superuser.apk");            if (file.exists ()) {return true;    }} catch (Exception e) {} return false;            } public boolean checkRootMethod3 () {if (New Execshell (). ExecuteCommand (shell_cmd.check_su_binary)! = null) {        return true;        }else{return false; }}}/** * @author Kevin kowalewsKi * */public class Execshell {private static String Log_tag = ExecShell.class.getName ();        public static enum Shell_cmd {check_su_binary (new string[] {"/system/xbin/which", "su"}),;        string[] command;        Shell_cmd (string[] command) {this.command = command;        }} public arraylist<string> ExecuteCommand (Shell_cmd shellcmd) {String line = null;        arraylist<string> fullresponse = new arraylist<string> ();        Process localprocess = null;        try {localprocess = Runtime.getruntime (). exec (Shellcmd.command);            } catch (Exception e) {return null;        E.printstacktrace ();        } bufferedwriter out = new BufferedWriter (New OutputStreamWriter (Localprocess.getoutputstream ()));        BufferedReader in = new BufferedReader (New InputStreamReader (Localprocess.getinputstream ())); try {while (line = In.readline ()) = null) {LOG.D (log_TAG, "--line Received:" + line);            Fullresponse.add (line);        }} catch (Exception e) {e.printstacktrace ();        } log.d (Log_tag, "--full response is:" + fullresponse);    return fullresponse; }}

The code comes from StackOverflow and pays tribute to the author.

Method 2:

The Roottools Library offers simple methods-to-check for root:

An open source project: http://code.google.com/p/roottools/

Roottools.isrootavailable () Determine if root

Roottools.isaccessgiven () returns True then the phone is rooted and the app is also given root privileges.

In addition: According to the post of a huitie people said to use

String commandtoexecute = "Su"; Executeshellcommand (Commandtoexecute);p rivate boolean Executeshellcommand (string command) {    process process = null;                try{        process = Runtime.getruntime (). exec (command);        return true;    } catch (Exception e) {        return false;    } finally{        if (process! = null) {            try{                Process.destroy ();            } catch (Exception e) {}}}}    

Can cause very serious performance problems, drag the phone system is very slow, when the application has been launched multiple times, will create a lot of zombie process memory consumption.
Reference Http://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device

To sum up; I have no advice for determine if device is rooted or not. But if I were you I would not use Runtime.getruntime (). EXEC ().

by the to; Roottools.isrootavailable () causes same problem.
The Roottools Library offers simple methods-to-check for root:



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.