安卓(Android) 怎么作为Root运行

2019年10月13日15:16:22安卓(Android) 怎么作为Root运行已关闭评论 259 925字阅读3分5秒

1.简介

安卓(Android) 怎么作为Root运行?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.io.DataOutputStream;
import java.io.File;

import java.io.IOException;

public class Main {
    public static Process runAsRoot(String... args) throws IOException {
        if (args == null || args.length == 0)
            return null;
        Process su = run("su");
        DataOutputStream out = new DataOutputStream(su.getOutputStream());
        for (String arg : args) {
            out.writeBytes(" ");
            out.writeBytes(arg);
        }
        out.writeBytes("\n");
        out.writeBytes("exit\n");
        out.flush();
        return su;
    }

    public static Process run(String executable, String... args)
            throws IOException {
        return run(executable, null, null, args);
    }

    public static Process run(String executable, File workingDirectory,
            String[] envp, String... args) throws IOException {
        if (args.length > 0) {
            String[] pargs = new String[args.length + 1];
            System.arraycopy(args, 0, pargs, 1, args.length);
            pargs[0] = executable;
            args = pargs;
        } else {
            args = new String[] { executable };
        }
        return Runtime.getRuntime().exec(args, envp, workingDirectory);
    }
}

 
继续阅读
编程十万个怎么办