|
|
@ -23,28 +23,63 @@ public class AdbUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void ShellList(ArrayList<String> shellList) {
|
|
|
|
public static void ShellList(boolean ifsu, ArrayList<String> shellList) {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// for (String cmd : shellList) {
|
|
|
|
// // 将整个命令包裹在单引号中,作为 -c 后的唯一参数
|
|
|
|
// Process process = Runtime.getRuntime().exec(cmd);
|
|
|
|
// String[] cmds = {
|
|
|
|
// // 处理输出和错误流
|
|
|
|
// "su",
|
|
|
|
// int i = process.waitFor();
|
|
|
|
// "-c",
|
|
|
|
// System.out.println(""+i);
|
|
|
|
// "rm -rf /sdcard/com.xypower.mpapp/tmp/*"
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// Process process = Runtime.getRuntime().exec(cmds);
|
|
|
|
|
|
|
|
// // 消费输入流(防止阻塞)
|
|
|
|
|
|
|
|
// new Thread(() -> {
|
|
|
|
|
|
|
|
// try (BufferedReader reader = new BufferedReader(
|
|
|
|
|
|
|
|
// new InputStreamReader(process.getInputStream())
|
|
|
|
|
|
|
|
// )) {
|
|
|
|
|
|
|
|
// while (reader.readLine() != null); // 静默消费
|
|
|
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }).start();
|
|
|
|
|
|
|
|
// // 消费错误流(关键!)
|
|
|
|
|
|
|
|
// new Thread(() -> {
|
|
|
|
|
|
|
|
// try (BufferedReader errReader = new BufferedReader(
|
|
|
|
|
|
|
|
// new InputStreamReader(process.getErrorStream())
|
|
|
|
|
|
|
|
// )) {
|
|
|
|
|
|
|
|
// String line;
|
|
|
|
|
|
|
|
// while ((line = errReader.readLine()) != null) {
|
|
|
|
|
|
|
|
// System.err.println("SU_ERROR: " + line); // 输出错误信息
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }).start();
|
|
|
|
|
|
|
|
// // 处理输出和错误流
|
|
|
|
|
|
|
|
// int i = process.waitFor();
|
|
|
|
|
|
|
|
// System.out.println("" + i);
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 使用 su 获取 root 权限
|
|
|
|
Process process = null;
|
|
|
|
Process process = Runtime.getRuntime().exec("su");
|
|
|
|
if (shellList != null && shellList.size() > 0) {
|
|
|
|
|
|
|
|
if (ifsu) {
|
|
|
|
|
|
|
|
// 使用 su 获取 root 权限
|
|
|
|
|
|
|
|
process = Runtime.getRuntime().exec("su");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
process = Runtime.getRuntime().exec("sh");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process == null) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取输出流,用于发送命令
|
|
|
|
// 获取输出流,用于发送命令
|
|
|
|
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
|
|
|
|
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < shellList.size(); i++) {
|
|
|
|
for (int i = 0; i < shellList.size(); i++) {
|
|
|
|
outputStream.writeBytes(shellList.get(i)+"\n");
|
|
|
|
outputStream.writeBytes(shellList.get(i) + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 执行命令(例如:列出 /data 目录)
|
|
|
|
// 执行命令(例如:列出 /data 目录)
|
|
|
|
|
|
|
|
|
|
|
@ -58,7 +93,6 @@ public class AdbUtil {
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
Log.d("Output", line); // 打印输出
|
|
|
|
Log.d("Output", line); // 打印输出
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 等待命令执行完成
|
|
|
|
// 等待命令执行完成
|
|
|
|
int i = process.waitFor();
|
|
|
|
int i = process.waitFor();
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|