从app [Rooted]运行shell命令

Tar*_*nfx 4 android root

在我的应用程序中,我想运行几个shell命令sand解释输出.这些命令基本上是在root电话上运行的.

我该怎么做?

sar*_*war 5

首先确保您需要的shell命令在Android中实际可用.假设您可以使用>重定向输出,我遇到了问题.

这个方法也适用于我相信v2.2的非root手机,但你应该检查API参考以确定.

try {
        Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(nfiq.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();
        chmod.waitFor();
        outputString =  output.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
Run Code Online (Sandbox Code Playgroud)

虽然它可能不是100%必要的,但是让进程等待exec完成process.waitFor()是个好主意,因为你说你关心输出.