3

为了澄清,我使用这段代码来获得我的应用程序的超级用户权限,这样我就可以访问 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”)。有什么想法吗?

4

3 回答 3

1

如果您只是在寻找已经在您的设备中运行的“超级用户”应用程序的权限,您只需要在您的主 java 文件中添加以下代码。

try {
        process p= Runtime.getRuntime().exec(su);
    }

catch (IOException e) {
      e.printStackTrace();
   }

是的,不用说设备必须已经root了!!!

于 2013-03-01T21:07:48.800 回答
1

您可以使用我的库来执行此操作。

https://code.google.com/p/roottools/

此外,如果您不想使用该库,则可以使用源代码,因此您可以撕下我的代码并在您的应用程序中使用它。

这是源代码的链接:

https://code.google.com/p/roottools/source/browse/#svn%2Ftrunk%2FStable%2FRootTools-sdk3-generic%2Fsrc%2Fcom%2Fstericson%2FRootTools

于 2012-09-17T13:42:23.913 回答
0

每次您打开一个要求 root 访问权限的控制台,即使用 启动它时su,如果您在上一个提示中选中“不要再问我”之类的内容,相应的超级用户应用程序将提示您或允许/拒绝它。

如果您只想询问(超级用户应用程序)一次,则必须保持根控制台打开,而不是调用dos.writeBytes("exit\n");. 然后将此会话保留在后台线程中,并在必要时使用它。

因此,要么确保用户在第一次提示时选中“不要再问我”,要么保持会话打开。

于 2012-11-17T13:23:58.267 回答