【发布时间】:2012-09-14 19:20:49
【问题描述】:
为了澄清,我使用此代码为我的应用程序获取超级用户权限,以便我可以访问 root 和诸如此类:
public String runProcess(String[] functs) {
DataOutputStream dos;
DataInputStream dis;
StringBuffer contents = new StringBuffer("");
String tempInput;
Process process;
try {
process = Runtime.getRuntime().exec(functs);
dos = new DataOutputStream(process.getOutputStream());
dis = new DataInputStream(process.getInputStream());
for (String command : functs) {
dos.writeBytes(command + "\n");
dos.flush();
while ((tempInput = dis.readLine()) != null)
contents.append(tempInput + "\n");
}
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return contents.toString();
}
虽然它工作得很好,但每当我打电话给runProcess(new String[] { "su", "-c", "some other command" }); 它总是要求超级用户许可。我看到市场上有很多根应用程序只需要在应用程序每次启动时获得一次超级用户权限,但我认为我不需要每次应用程序调用函数时都向用户询问超级用户权限这需要SU。所以我的问题是,我如何在应用启动时提示用户给我一次 SU 权限,而不必为每个与 SU 相关的操作不断地请求它?
编辑:我知道我可以运行我的方法/Runtime.getRuntime().exec() 方法,而无需每次都在其中输入“su”,但这仅适用于非 su 相关操作(即 exec("ps" ) 或 exec("ls")。有什么想法吗?
【问题讨论】:
标签: android process runtime exec root