手机获得root权限判断网上有很多,在这里就贴上一个和网上一样的
/**
* 判断当前手机是否有ROOT权限
* @return
*/
public boolean isRoot(){
boolean bool = false;
try{
if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){
bool = false;
} else {
bool = true;
}
Log.d(TAG, "bool = " + bool);
} catch (Exception e) {
}
return bool;
}
app是否获得root权限的方法网上也是有的,但好像不是太给力,我的方法运用adb命令来验证是否root ,如果root就会遍历data/data/包,并且有输出流,反之大家都懂,上代码
/**
* 判断app是否获得root权限
*
* @return
*/
public static boolean appRoot() {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("ls /data/data/\n");
os.writeBytes("exit\n");
os.flush();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
Log.e("tag",result);
Log.e("tag","after Log string buffer");
if (result.contains( "com.android.phone" )){
return true;
}
} catch (IOException e) {
return false;
}
return false;
}