android判斷手機是否root

public boolean checkRootMethod1() {

String buildTags = android.os.Build.TAGS;

if (buildTags != null && buildTags.contains("test-keys")) {

returntrue;

}

returnfalse;

}



public boolean checkRootMethod2() {

try {

File file =new File("/system/app/Superuser.apk");

if (file.exists()) {

returntrue;

}

} catch (Exception e) {

}

returnfalse;

}

public boolean checkRootMethod3() {

int kSystemRootStateUnknow = -1;

int kSystemRootStateDisable = 0;

int kSystemRootStateEnable = 1;

int systemRootState = kSystemRootStateUnknow;

if (systemRootState == kSystemRootStateEnable) {

returntrue;

} else if (systemRootState == kSystemRootStateDisable) {



returnfalse;

}

File f = null;

final String kSuSearchPaths[] = {"/system/bin/", "/system/xbin/", "/system/sbin/",

"/sbin/","/vendor/bin/" };

try {

for (int i = 0; i < kSuSearchPaths.length; i++) {

f = new File(kSuSearchPaths[i] + "su");

if (f !=null && f.exists()) {

systemRootState = kSystemRootStateEnable;

returntrue;

}

}

} catch (Exception e) {

}

systemRootState = kSystemRootStateDisable;

returnfalse;

}


public boolean checkRootMethod4() {

if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) !=null) {

returntrue;

} else {

returnfalse;

}

}



package com.example.test;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;


import android.util.Log;


public class ExecShell {


private static final 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 executeCommand(SHELL_CMD shellCmd) {
String line = null;
ArrayList fullResponse = new ArrayList();
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 was: " + fullResponse);


return fullResponse;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章