ADB命令
说到ADB命令,先给大家安利一个网站。awesome-adb
网站内容部分截图:
这个网站几乎包含了所有adb指令语法用法,详细的教程和例子可以帮助我们快速开发和测试Android应用。
在Android App中使用su权限执行adb命令
入正题,具体实现如下。
贴上代码:
public static String exec(String command) {
Process process = null;
BufferedReader reader = null;
InputStreamReader is = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
is = new InputStreamReader(process.getInputStream());
reader = new BufferedReader(is);
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
int read;
char[] buffer = new char[4096];
StringBuilder output = new StringBuilder();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
process.waitFor();
return output.toString();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
} finally {
try {
if (os != null) {
os.close();
}
if (reader != null) {
reader.close();
}
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}