您的位置:首页 > 其它

是否有Root权限

2016-04-27 11:11 316 查看
Process process = null;
DataOutputStream os = null;
BufferedInputStream br = null;
try {
String cmd = "ls /data/app";
// 切换到root帐号
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();

br = new BufferedInputStream(process.getErrorStream());
StringBuffer text = new StringBuffer();
int ch;
while ((ch = br.read()) != -1) {
text.append((char) ch);
}

int exitValue = process.waitFor();
String ret = text.toString();
boolean permission = false;
if (ret == null || ret.trim().length() == 0) {
permission = true;
}

if (exitValue == 0) {
if (Build.VERSION.SDK_INT == 23) {
return true;
}

if (permission) {
return true;
}
return false;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
if (br != null) {
br.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: