Android ShellUtils手机管理器

1. Android ShellUtils手机管理器

Android Shell工具类,可用于检查系统root权限,并在shell或root用户下执行shell命令。如:

checkRootPermission() 检查root权限 。execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) shell 环境执行命令,第二个参数表示是否root权限执行 execCommand(String command, boolean isRoot) shell环境执行命令。

1.1. 代码

javascript 复制代码
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class ShellUtils {
    public static final String COMMAND_SU = "su";
    public static final String COMMAND_SH = "sh";
    public static final String COMMAND_EXIT = "exit\n";
    public static final String COMMAND_LINE_END = "\n";

    public ShellUtils() {
    }

    public static boolean checkRootPermission() {
        return execCommand("echo root", true, false).result == 0;
    }

    public static CommandResult execCommand(String command, boolean isRoot) {
        return execCommand(new String[]{command}, isRoot, true);
    }

    public static CommandResult execCommand(List<String> commands, boolean isRoot) {
        return execCommand(commands == null ? null : (String[])commands.toArray(new String[0]), isRoot, true);
    }

    public static CommandResult execCommand(String[] commands, boolean isRoot) {
        return execCommand(commands, isRoot, true);
    }

    public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(new String[]{command}, isRoot, isNeedResultMsg);
    }

    public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(commands == null ? null : (String[])commands.toArray(new String[0]), isRoot, isNeedResultMsg);
    }

    public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
        int result = -1;
        if (commands != null && commands.length != 0) {
            Process process = null;
            BufferedReader successResult = null;
            BufferedReader errorResult = null;
            StringBuilder successMsg = null;
            StringBuilder errorMsg = null;
            DataOutputStream os = null;

            try {
                process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
                os = new DataOutputStream(process.getOutputStream());
                String[] var13 = commands;
                int var12 = commands.length;

                String s;
                for(int var11 = 0; var11 < var12; ++var11) {
                    s = var13[var11];
                    if (s != null) {
                        os.write(s.getBytes());
                        os.writeBytes("\n");
                        os.flush();
                    }
                }

                os.writeBytes("exit\n");
                os.flush();
                result = process.waitFor();
                if (isNeedResultMsg) {
                    successMsg = new StringBuilder();
                    errorMsg = new StringBuilder();
                    successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                    while((s = successResult.readLine()) != null) {
                        successMsg.append(s);
                    }

                    while((s = errorResult.readLine()) != null) {
                        errorMsg.append(s);
                    }
                }
            } catch (IOException var24) {
                var24.printStackTrace();
            } catch (Exception var25) {
                var25.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }

                    if (successResult != null) {
                        successResult.close();
                    }

                    if (errorResult != null) {
                        errorResult.close();
                    }
                } catch (IOException var23) {
                    var23.printStackTrace();
                }

                if (process != null) {
                    process.destroy();
                }

            }

            return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString());
        } else {
            return new CommandResult(result, (String)null, (String)null);
        }
    }

    public static class CommandResult {
        public int result;
        public String successMsg;
        public String errorMsg;

        public CommandResult(int result) {
            this.result = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }
    }
}

1.2. 使用

(1)关机

javascript 复制代码
public void ShutDown(View view) {
   ShellUtils.execCommand("reboot -p", true);
}

(2)重启

javascript 复制代码
public void Reboot(View view) {
   ShellUtils.execCommand("reboot", true);
}

(3)快速重启

javascript 复制代码
public void FastReboot(View view) {
     ShellUtils.execCommand("busybox killall system_server", true);
}

(4)进入刷机模式

javascript 复制代码
public void Recovery(View view) {
    ShellUtils.execCommand("reboot recovery", true);
}

(5)进入引导模式

javascript 复制代码
public void FastBoot(View view) {
    ShellUtils.execCommand("reboot bootloader", true);
}

1.3. adb shell input keyevent 控制按键输入的数值

adb shell的功能很强大,可以使用很多功能,今天我们说下通过控制按键输入:adb shell input keyevent xx ,具体数值xx如下

KEYCODE_CALL 进入拨号盘 5

KEYCODE_ENDCALL 挂机键 6

KEYCODE_HOME 按键Home 3

KEYCODE_MENU 菜单键 82

KEYCODE_BACK 返回键 4

KEYCODE_SEARCH 搜索键 84

KEYCODE_CAMERA 拍照键 27

KEYCODE_FOCUS 拍照对焦键 80

KEYCODE_POWER 电源键 26

KEYCODE_NOTIFICATION 通知键 83

KEYCODE_MUTE 话筒静音键 91

KEYCODE_VOLUME_MUTE 扬声器静音键 164

KEYCODE_VOLUME_UP 音量增加键 24

KEYCODE_VOLUME_DOWN 音量减小键 25

控制键

KEYCODE_ENTER 回车键 66

KEYCODE_ESCAPE ESC键 111

KEYCODE_DPAD_CENTER 导航键 确定键 23

KEYCODE_DPAD_UP 导航键 向上 19

KEYCODE_DPAD_DOWN 导航键 向下 20

KEYCODE_DPAD_LEFT 导航键 向左 21

KEYCODE_DPAD_RIGHT 导航键 向右 22

KEYCODE_MOVE_HOME 光标移动到开始键 122

KEYCODE_MOVE_END 光标移动到末尾键 123

KEYCODE_PAGE_UP 向上翻页键 92

KEYCODE_PAGE_DOWN 向下翻页键 93

KEYCODE_DEL 退格键 67

KEYCODE_FORWARD_DEL 删除键 112

KEYCODE_INSERT 插入键 124

KEYCODE_TAB Tab键 61

KEYCODE_NUM_LOCK 小键盘锁 143

KEYCODE_CAPS_LOCK 大写锁定键 115

KEYCODE_BREAK Break/Pause键 121

KEYCODE_SCROLL_LOCK 滚动锁定键 116

KEYCODE_ZOOM_IN 放大键 168

KEYCODE_ZOOM_OUT 缩小键 169

利用命令"adb shell input keyevent <键值>"可以实现自动化。例如"adb shell input keyevent 3"就可以按下Home键。]

(1)执行返回:adb shell input keyevent 4

(2)执行灭屏亮屏:adb shell input keyevent 26

(3)执行解锁屏幕:adb shell input keyevent 82

相关推荐
scan7242 小时前
长期记忆存储在数据库里
android
xingpanvip2 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
卷Java3 小时前
GPTQ vs AWQ vs GGUF:模型量化工具横向测评
开发语言·windows·python
x-cmd4 小时前
[260429] x-cmd v0.9.1:一键开启 DeepSeek-V4-Pro Max 模式 + 1M 上下文;顺手重构了 uuid 模块
windows·重构·uuid·claude·curl·x-cmd·deepseek-v4-pro
今夕资源网4 小时前
Windows 上安装 Claude Code并且接入DeepSeekV4-Pro的Max模式和激活1M上下文
windows
儿歌八万首5 小时前
Jetpack Compose 实战:实现一个动态平滑折线图
android·折线图·compose
HLJ洛神千羽5 小时前
电脑右下角显示【周几或星期几】和【上下午】方法
windows
ITHAOGE155 小时前
下载 | Windows Server 2025官方原版ISO映像!(4月更新、标准版、数据中心版、26100.32690)
服务器·windows·科技·微软·电脑
花先锋队长6 小时前
鸿蒙6.1加持菜鸟App:地理围栏+实况窗,靠近驿站自动提醒,取件不再遗漏
华为·智能手机·harmonyos
专注VB编程开发20年7 小时前
Windows API 所有老式结构体4字节对齐,但是64位VBA,Twinbasic弄成了8字节对齐,大BUG
windows·bug