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权限命令的需求。

相关推荐
乔宕一1 小时前
windows SSH服务修改SSH登陆后的默认终端
运维·windows·ssh
嵌入式-老费1 小时前
vivado hls的应用(第一个axi接口的ip)
linux·服务器·tcp/ip
旺仔.2912 小时前
Linux系统基础详解(二)
linux·开发语言·网络
x***r1512 小时前
Notepad++ 8.6 安装教程:详细步骤+自定义安装路径(附注意事项)
linux·前端·javascript
big_rabbit05022 小时前
JVM堆内存查看命令
java·linux·算法
bwz999@88.com2 小时前
联想SR5507X04安装ubuntu-24.04.4 server,采用 Linux 原生mdadm(mdraid)软 RAID+LVM分区
运维·服务器
Canicer2 小时前
OpenClaw搭配Coze工作流实现全自动发布文章至WordPress网站!
运维·服务器
王小义笔记2 小时前
WSL(Linux)如何安装conda
linux·运维·conda
Fairy要carry2 小时前
面试10-Agent 团队协议的管理
运维·服务器·网络
袁庭新2 小时前
M系列芯片Mac上通过Homebrew一键安装/卸载Nginx并上线项目全指南
运维·nginx·macos·袁庭新·袁庭新ai