/** * Android shell 命令执行器,支持无限个命令串型执行(需要有root权限!!) * 

*

* HOW TO USE? * Example:修改开机启动动画。把/sdcard/Download目录下的bootanimation.zip文件拷贝到 * /system/media目录下并修改bootanimation.zip的权限为777。 *

*

 *      int result = new ShellCommandExecutor() *                  .addCommand("mount -o remount,rw /system") *                  .addCommand("cp /sdcard/Download/bootanimation.zip /system/media") *                  .addCommand("cd /system/media") *                  .addCommand("chmod 777 bootanimation.zip") *                  .execute(); * 
 * * @author AveryZhong. */public class ShellCommandExecutor {    private static final String TAG = "ShellCommandExecutor";    private StringBuilder mCommands;    public ShellCommandExecutor() {        mCommands = new StringBuilder();    }    public int execute() {        return execute(mCommands.toString());    }    public ShellCommandExecutor addCommand(String cmd) {        if (TextUtils.isEmpty(cmd)) {            throw new IllegalArgumentException("command can not be null.");        }        mCommands.append(cmd);        mCommands.append("\n");        return this;    }    private static int execute(String command) {        int result = -1;        DataOutputStream dos = null;        try {            Process p = Runtime.getRuntime().exec("su");            dos = new DataOutputStream(p.getOutputStream());            Log.i(TAG, command);            dos.writeBytes(command + "\n");            dos.flush();            dos.writeBytes("exit\n");            dos.flush();            p.waitFor();            result = p.exitValue();        } catch (Exception e) {            e.printStackTrace();        } finally {            if (dos != null) {                try {                    dos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return result;    }}

更多相关文章

  1. android mtk平台默认输入法
  2. Android(安卓)framework修改----关屏动画效果
  3. Android官方命令深入分析之Device Monitor
  4. 【Android】Android(安卓)9.0 隐藏虚拟按键跟状态栏,除去google
  5. Android命令行测试BT,WIFI,Sensor工作状态
  6. 修改android桌面图标默认大小
  7. Android(安卓)Studio 配置SVN 及 代码管理
  8. Android中density如何设置
  9. Android执行shell命令

随机推荐

  1. 微服务架构概述
  2. 服务端开发指南与最佳实战 | 数据存储技
  3. JavaScript测试教程-part 2:引入 Enzyme
  4. 单例模式 - 只有一个实例
  5. 手撕JS(可能持续更新···)
  6. JavaScript 测试教程 part 1:用 Jest 进行
  7. SOA 对比微服务架构
  8. 服务端开发指南与最佳实战 | 数据存储技
  9. vtp基本命令
  10. 原型模式 - 通过复制生成实例