Android 常用工具類之RuntimeUtil

來源:互聯網
上載者:User

標籤:

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] + "\n");            writer.flush();        }        writer.write("exit \n");        writer.flush();    }}

 

Android 常用工具類之RuntimeUtil

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.