Android

个人Android学习总结


Android 代码执行adb shell 指令

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

页面列表

ITEM_HTML