Two ways to check if Android has root access

Source: Internet
Author: User
Tags root access

now the Android phone root privilege can be said to be easy to get, and about to determine whether the phone has root method, you understand? If the app has some special features that require root access, you'll need to determine if it's root. Don't know if root means the phone isn't safe? After all, think of a video tutorial on exactly how you can learn Android app development . Two methods are described below:

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/12/tz2PlFQX8d3SH-sUAADw5Psp0Hs406.png "height=" 216 "Width=" 388 "style=" border:0px;/>

Method 1:

/**
* @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;
}
}
}< /span>
/**
* @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.

String commandtoexecute = "su";
Executeshellcommand (commandtoexecute);
Private 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) {
}
}
}
}

PS: It is possible to use these methods to drag the phone system very slowly, when the application has been launched multiple times, will create a lot of zombie process memory consumption.


there's more . Mobile Internet Tutorials information, etc. please pay attention to e Mentor Network.



Two ways to check if Android has root access

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.