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. 代碼
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)關機
public void ShutDown(View view) {ShellUtils.execCommand("reboot -p", true);
}
(2)重啟
public void Reboot(View view) {ShellUtils.execCommand("reboot", true);
}
(3)快速重啟
public void FastReboot(View view) {ShellUtils.execCommand("busybox killall system_server", true);
}
(4)進入刷機模式
public void Recovery(View view) {ShellUtils.execCommand("reboot recovery", true);
}
(5)進入引導模式
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