Android判斷是否Root方法介紹
為了照顧那些著急的同學(xué),先直接給出結(jié)論:
private static final String[] rootRelatedDirs = new String[]{
"/su", "/su/bin/su", "/sbin/su",
"/data/local/xbin/su", "/data/local/bin/su", "/data/local/su",
"/system/xbin/su",
"/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
"/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr",
"/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
"/system/bin/conbb", "/system/xbin/conbb"};
public static boolean hasRootPrivilege() {
boolean hasRootDir = false;
String[] rootDirs;
int dirCount = (rootDirs = rootRelatedDirs).length;
for (int i = 0; i < dirCount; ++i) {
String dir = rootDirs[i];
if ((new File(dir)).exists()) {
hasRootDir = true;
break;
}
}
return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
}好,接下來(lái)我們來(lái)看看到底是如何得到上述的解決方案的。首先,這是既有的判斷root權(quán)限的方案,即判定兩個(gè)root權(quán)限相關(guān)文件夾是否存在,以及當(dāng)前賬戶是否具備訪問(wèn)其內(nèi)容的權(quán)限,如果都成立,那么就認(rèn)為當(dāng)前賬號(hào)具備root權(quán)限。然而,這種root方案在一些情況下不能很好地發(fā)揮作用。
/**
* 判斷Android設(shè)備是否擁有Root權(quán)限
*/
public class RootCheck {
private final static String TAG = "RootUtil";
public static boolean isRoot() {
String binPath = "/system/bin/su";
String xBinPath = "/system/xbin/su";
if (new File(binPath).exists() && isExecutable(binPath))
return true;
if (new File(xBinPath).exists() && isExecutable(xBinPath))
return true;
return false;
}
private static boolean isExecutable(String filePath) {
Process p = null;
try {
p = Runtime.getRuntime().exec("ls -l " + filePath);
// 獲取返回內(nèi)容
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = in.readLine();
Log.i(TAG, str);
if (str != null && str.length() >= 4) {
char flag = str.charAt(3);
if (flag == 's' || flag == 'x')
return true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (p != null) {
p.destroy();
}
}
return false;
}
}然后我就找到了如下方案,該方案號(hào)稱是騰訊bugly的root權(quán)限判斷方案:
private static final String[] a = new String[]{"/su", "/su/bin/su", "/sbin/su",
"/data/local/xbin/su", "/data/local/bin/su", "/data/local/su",
"/system/xbin/su", "/system/bin/su", "/system/sd/xbin/su",
"/system/bin/failsafe/su", "/system/bin/cufsdosck",
"/system/xbin/cufsdosck", "/system/bin/cufsmgr",
"/system/xbin/cufsmgr", "/system/bin/cufaevdd",
"/system/xbin/cufaevdd", "/system/bin/conbb",
"/system/xbin/conbb"};
public static boolean p() {
boolean var0 = false;
String[] var1 = a;
int var2 = a.length;
for(int var3 = 0; var3 < var2; ++var3) {
String var4 = var1[var3];
if ((new File(var4)).exists()) {
var0 = true;
break;
}
}
return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}當(dāng)然,本人生性多疑,偶像是曹操曹丞相,所以自然不能人云亦云,還是實(shí)際確認(rèn)一下bugly實(shí)際上是否是這樣實(shí)現(xiàn)的,以及確保bugly在新的版本中有沒(méi)有對(duì)該方案有進(jìn)一步的改進(jìn)。
然后我就到bugly官網(wǎng),下載了其最新發(fā)布的jar包,筆者下載時(shí)最新的版本為4.4.4,然后直接解壓,然后在解壓的目錄中搜索“test-keys”內(nèi)容。
grep -r test-keys "D:\迅雷下載\Bugly_v3.4.4
最后找到了對(duì)應(yīng)的文件位置和對(duì)應(yīng)方法:com\tencent\bugly\crashreport\common\info\b.class
private static final String[] a = new String[]{"/su", "/su/bin/su",
"/sbin/su", "/data/local/xbin/su", "/data/local/bin/su",
"/data/local/su", "/system/xbin/su", "/system/bin/su",
"/system/sd/xbin/su", "/system/bin/failsafe/su",
"/system/bin/cufsdosck", "/system/xbin/cufsdosck",
"/system/bin/cufsmgr", "/system/xbin/cufsmgr",
"/system/bin/cufaevdd", "/system/xbin/cufaevdd",
"/system/bin/conbb", "/system/xbin/conbb"};
public static boolean l() {
boolean var0 = false;
String[] var1;
int var2 = (var1 = a).length;
for(int var3 = 0; var3 < var2; ++var3) {
String var4 = var1[var3];
if ((new File(var4)).exists()) {
var0 = true;
break;
}
}
return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}然后分析一下對(duì)應(yīng)變量的意思,我們就能還原出騰訊判斷Root的代碼,即我們開(kāi)頭所貼出的解決方案:
private static final String[] rootRelatedDirs = new String[]{
"/su", "/su/bin/su", "/sbin/su",
"/data/local/xbin/su", "/data/local/bin/su", "/data/local/su",
"/system/xbin/su",
"/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
"/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr",
"/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
"/system/bin/conbb", "/system/xbin/conbb"};
public static boolean hasRootPrivilege() {
boolean hasRootDir = false;
String[] rootDirs;
int dirCount = (rootDirs = rootRelatedDirs).length;
for (int i = 0; i < dirCount; ++i) {
String dir = rootDirs[i];
if ((new File(dir)).exists()) {
hasRootDir = true;
break;
}
}
return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
}到此這篇關(guān)于Android判斷是否Root方法介紹的文章就介紹到這了,更多相關(guān)Android判斷Root內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android編程實(shí)現(xiàn)局部界面動(dòng)態(tài)切換的方法
這篇文章主要介紹了android編程實(shí)現(xiàn)局部界面動(dòng)態(tài)切換的方法,以實(shí)例形式較為詳細(xì)的分析了Android局部切換的布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android自定義View實(shí)現(xiàn)自動(dòng)轉(zhuǎn)圈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)自動(dòng)轉(zhuǎn)圈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
解決Eclipse創(chuàng)建android項(xiàng)目無(wú)法正常預(yù)覽布局文件問(wèn)題的方法
這篇文章主要介紹了解決Eclipse創(chuàng)建android項(xiàng)目無(wú)法正常預(yù)覽布局文件問(wèn)題的方法,需要的朋友可以參考下2015-12-12
Android來(lái)電攔截的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android來(lái)電攔截的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android將內(nèi)容分享到QQ和微信實(shí)例代碼
這篇文章主要介紹了Android將內(nèi)容分享到QQ和微信實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
分析Android多主題顏色的相關(guān)問(wèn)題
這篇文章總結(jié)了在Android開(kāi)發(fā)多主題顏色的時(shí)候會(huì)遇到的一些問(wèn)題,然后給出解決方案,讓大家可以解決問(wèn)題,有需要的下面一起來(lái)看看吧。2016-08-08

