登录
  • 欢迎访问 Sharezer Blog

Runtime.getRuntime().exec获取root权限,并执行命令

Adb sharezer 7332次浏览 已收录 2个评论
public String execRootCmd(String cmd) {
    String result = "";
    DataOutputStream dos = null;
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        Log.i(TAG, cmd);
        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        String line = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
        while ((line = br.readLine()) != null) {
            Log.d(TAG, "result:" + line);
            result += line;
        }
        p.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Runtime.getRuntime().exec获取root权限,并执行命令
喜欢 (21)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 很荣幸来访您的博客,留言只是证明我来过!
    衣皇后2017-04-13 13:33 回复 未知操作系统 | 未知浏览器
  2. 世事无常,但这个博客定能永保辉煌!
    yihuanghou2017-04-18 15:03 回复 未知操作系统 | 未知浏览器