登录
  • 欢迎访问 Sharezer Blog

Android 执行su命令

Adb sharezer 2494次浏览 已收录 0个评论
// 执行命令并且输出结果
private String execRootCmd(String cmd) {
    Log.d(TAG, "cmd: " + 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());
        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();

        String line = "";
        BufferedReader din = new BufferedReader(new InputStreamReader(dis));
        while ((line = din.readLine()) != null) {
            Log.d(TAG, line);
            result += line;
        }
        Log.d(TAG, "end");
        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协议进行授权 , 转载请注明Android 执行su命令
喜欢 (2)
发表我的评论
取消评论

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

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址