Android app调用系统重启、关机、获取root权限

我们在开发智能硬件时,可能会遇到软件方面的数据错误或者硬件方面的问题,我们会在app中预留远程重启智能系统的接口,当我们后台推送给app端远程重启命令时,app端需要立即调用系统重启方法。

/**
     * 执行命令
     *
     * @param command 1、获取root权限 "chmod 777 "+getPackageCodePath()
     *                2、关机 reboot -p
     *                3、重启 reboot
     */
    public static boolean execCmd(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/d38825/article/details/78893020