android su执行命令

通过Runtime.getRuntime().exec("su")启动su进程,然后使用DataOutputStream向其写入要执行的命令。命令执行完成后需要调用process.waitFor()等待进程结束,并妥善关闭所有流资源

复制代码
import java.io.DataOutputStream;

public class SuCommandExecutor {
    private static final String TAG = "SuCommandExecutor";
    
    /**
     * 执行需要root权限的单个命令
     * @param command 要执行的命令
     * @return 命令执行结果
     */
    public static String execSuCmd(String command) {
        String output = "";
        Process process = null;
        DataOutputStream os = null;
        DataInputStream is = null;
        
        try {
            // 启动su进程获取root权限
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            
            // 写入要执行的命令
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            
            // 等待命令执行完成
            int exitCode = process.waitFor();
            
            // 读取命令输出
            is = new DataInputStream(process.getInputStream());
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            output = new String(buffer);
            
        } catch (Exception e) {
            e.printStackTrace();
            output = "Exception: " + e.getMessage();
        } finally {
            // 关闭所有流资源
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return output;
    }
    
    /**
     * 静默执行root命令(不读取输出)
     * @param command 要执行的命令
     * @return 执行结果代码
     */
    public static int execRootCmdSilent(String command) {
        int result = -1;
        DataOutputStream dos = null;
        
        try {
            Process process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes(command + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            process.waitFor();
            result = process.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    
    /**
     * 测试设备是否具有su执行权限
     * @return 测试结果
     */
    public static boolean testSuPermission() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("id\n");
            dos.writeBytes("exit\n");
            dos.flush();
            int exitCode = process.waitFor();
            return exitCode == 0;
        } catch (Exception e) {
            return false;
        }
    }
    
    public static void main(String[] args) {
        // 测试su权限
        boolean hasPermission = testSuPermission();
        System.out.println("设备是否具有su权限: " + hasPermission);
        
        if (hasPermission) {
            // 执行示例命令
            String result = execSuCmd("ls -l /system");
            System.out.println("命令执行结果: " + result);
        }
    }
}

import java.io.DataOutputStream;

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream dos = new DataOutputStream(process.getOutputStream());
                    dos.writeBytes("echo 72 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio72/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 78 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio78/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 98 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio98/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 97 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio97/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 62 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio62/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 63 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio63/direction"+"\n");
                    dos.flush();
        
                    dos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

关键注意事项

  • 设备要求‌:执行su命令前必须确认设备已root,且应用获得了su执行权限。
  • 权限申请‌:首次执行su命令时,系统会弹出授权对话框请求用户确认。
  • 异常处理‌:必须妥善处理IOException等异常情况,特别是在某些Android主板上可能出现执行异常4。
  • 资源管理‌:使用完毕后必须关闭DataOutputStream和Process等资源,避免内存泄漏。
  • 系统限制‌:普通应用无法直接访问系统自带的su程序,需要通过SuperSU等工具或系统root来获取权限。

该实现提供了完整的su命令执行功能,包括权限测试、命令执行和异常处理,能够满足Android应用中执行root权限命令的需求。

相关推荐
chlk1238 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑9 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件9 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒10 小时前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号19 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI1 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
Sinclair2 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing2 天前
WSL+Cpp开发环境配置
linux