• Android 常用工具类之RuntimeUtil


    public class RuntimeUtil {
    
        /** 通过查询su文件的方式判断手机是否root */
        public static boolean hasRootedSilent() {
            return new File("/system/bin/su").exists()
                    || new File("/system/xbin/su").exists()
                    || new File("/system/sbin/su").exists()
                    || new File("/sbin/su").exists()
                    || new File("/vendor/bin/su").exists();
        }
    
        /** 通过执行命令的方式判断手机是否root, 会有申请root权限的对话框出现 */
        public static boolean hasRooted() {
            return execSilent("echo test");
        }
    
        /** 执行命令获取结果集 */
        public static List<String> exec(String cmd) {
            List<String> dataList = null;
            BufferedWriter writer = null;
            BufferedReader reader = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                runCmd(writer, cmd);
                process.waitFor();
                dataList = new ArrayList<>();
                String content;
                while (null != (content = reader.readLine())) {
                    dataList.add(content);
                }
            } catch (Exception e) {
                //e.printStackTrace();
            } finally {
                closeCloseable(reader, writer);
                if (process != null) process.destroy();
            }
            return dataList;
        }
    
        /** 执行一组命令 */
        public static void execSilent(String... cmd) {
            BufferedWriter writer = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                runCmd(writer, cmd);
                process.waitFor();
            } catch (Exception e) {
                // e.printStackTrace();
            } finally {
                closeCloseable(writer);
                if (process != null) process.destroy();
            }
        }
    
        /** 判断进程是否正在运行 */
        public static boolean isProcessRunning(String processName) {
            List<String> processList = exec("ps");
            for (int i = 1; i < processList.size(); i++) {
                if (processList.get(i).endsWith(processName)) {
                    return true;
                }
            }
            return false;
        }
    
        /** 判断是否成功执行 */
        public static boolean execSilent(String cmd) {
            boolean result = false;
            BufferedWriter writer = null;
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("su");
                writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                runCmd(writer, cmd);
                process.waitFor();
                Log.d("runtime", "onCreate: process.exitValue()  " + process.exitValue());
                result = process.exitValue() == 0;
            } catch (Exception e) {
                // e.printStackTrace();
            } finally {
                closeCloseable(writer);
                if (process != null) process.destroy();
            }
            return result;
        }
    
        // 关闭流文件
        private static void closeCloseable(Closeable... closeable) {
            for (int i = 0; i < closeable.length; i++) {
                if (null != closeable) {
                    try {
                        closeable[i].close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        // 执行命令
        private static void runCmd(BufferedWriter writer, String... cmd) throws IOException {
            for (int i = 0; i < cmd.length; i++) {
                writer.write(cmd[i] + "
    ");
                writer.flush();
            }
            writer.write("exit 
    ");
            writer.flush();
        }
    }
  • 相关阅读:
    我喜欢的乐队-Descending
    SQL合并时间段的问题
    基本字符串相关函数,基本宏,内存相关函数,类型转换函数实现合集
    centos7.4 安装 .net core 2.2
    在Global.asax中 注册Application_Error事件 捕获全局异常
    一般后台系统数据库 用户权限设计
    API接口利用ActionFilterAttribute实现接口耗时检测
    Git 一般性操作
    tasks.json 配置 解决vscode控制台乱码问题
    iview发布到IIS 路由问题
  • 原文地址:https://www.cnblogs.com/Westfalen/p/5401423.html
Copyright © 2020-2023  润新知