Android 代码执行adb shell 指令
<h1>Android 代码执行adb shell 指令</h1>
<p>#</p>
<pre><code class="language-java"> /***************************************** adb shell 指令 *********************************************/
private String getCpuInfo() {
Log.d(TAG, &quot;/system/bin/top -m 15 -n 1 -s cpu&quot;);
String[] cmds = &quot;/system/bin/top -m 15 -n 1 -s cpu&quot;.split(&quot; &quot;);
return runCommand(cmds);
}
private String getMemInfo() {
Log.d(TAG, &quot;/system/bin/top -m 15 -n 1 -s rss&quot;);
String[] cmds = &quot;/system/bin/top -m 15 -n 1 -s rss&quot;.split(&quot; &quot;);
return runCommand(cmds);
}
private String getDumpInfo() {
Log.d(TAG, &quot;dumpsys meminfo&quot;);
String[] cmds = &quot;/system/bin/dumpsys meminfo&quot;.split(&quot; &quot;);
return runCommand(cmds);
}</code></pre>
<pre><code class="language-java">private String runCommand(String... cmd) {
String sb = &quot;&quot;;
InputStreamReader input = null;
try {
java.lang.Process logcat = new ProcessBuilder(cmd)
.redirectErrorStream(true).start();
try {
logcat.getOutputStream().close();
} catch (IOException e) {
}
try {
logcat.getErrorStream().close();
} catch (IOException e) {
}
input = new InputStreamReader(logcat.getInputStream(), &quot;UTF-8&quot;);
BufferedReader reader = new BufferedReader(input);
String line = null;
while ((line = reader.readLine()) != null) {
sb += line + &quot;\n&quot;;
// Log.d(TAG, &quot;line:&quot; + line);
}
} catch (IOException e) {
Log.e(TAG, &quot;Error running logcat&quot;, e);
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
}
}
return sb.toString();
}</code></pre>