当我尝试从具有系统权限的程序中运行 su 命令时,我在 stderr 上返回了此错误:
uid 1000 不允许 su
我将共享 uid 设置为 android.uid.system 或 android.uid.shell 但它不会改变任何事情。该设备据称已植根。我真的不明白会发生什么。
这是代码:
private boolean runAsRoot(String cmd, File workdir) throws IOException {
Log.i(LOG_TAG, "Execute command as root: " + cmd);
Process p = Runtime.getRuntime().exec(new String[] {"su", "-c", cmd});
if (p == null) {
Log.e(LOG_TAG, "cannot create process for " + cmd);
return false;
}
int exit = -1;
try {
exit = p.waitFor();
InputStreamReader isr;
if (exit == 0) {
isr = new InputStreamReader(p.getInputStream());
} else {
isr = new InputStreamReader(p.getErrorStream());
}
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
if (sb.length() > 0) {
sb.append('\n');
}
sb.append(line);
}
String string = sb.toString();
Log.l(LOG_TAG, exit == 0 ? Level.INFO : Level.SEVERE, string);
} catch (InterruptedException e) {
Log.e(LOG_TAG, e);
}
return exit == 0;
}