Android

个人Android学习总结


Java代码执行adb shell 命令

<h1>Java代码执行adb shell 命令</h1> <pre><code> /** * 执行shell命令 * * @param cmd */ private void execShellCmd(String cmd) { SkyLog.d("execShellCmd === " + cmd); try { // 申请获取root权限,这一步很重要,不然会没有作用 Process process = Runtime.getRuntime().exec("su"); // 获取输出流 OutputStream outputStream = process.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream( outputStream); dataOutputStream.writeBytes(cmd); dataOutputStream.flush(); dataOutputStream.close(); outputStream.close(); } catch (Throwable t) { t.printStackTrace(); } }</code></pre> <pre><code class="language-java">private void printLogcat(){ if(SystemProperties.getBoolean("persist.sys.logcat", true)){ if (null == testThread) { testThread = new HandlerThread("skyLogcat"); testThread.start(); if (null == testHandler) { testHandler = new Handler(testThread.getLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case TestMsg.LOGCAT: long time = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = format.format(time); SkyLog.d("currentTimeMillis: " + fileName); int result = SkyArmCmdExeManager.getManager().execSystemCommand("/system/bin/logcat -v time &gt; /sdcard/autologcat/" + fileName +".txt &amp;"); SkyLog.d("currentTimeMillis: " + time + ",create /sdcard/autologcat/ result : " + result); if(result != 0){ count ++ ; testHandler.removeMessages(TestMsg.LOGCAT); if(count &lt; 15) testHandler.sendEmptyMessageDelayed(TestMsg.LOGCAT, 200); } break; default: break; } } }; testHandler.sendEmptyMessageDelayed(TestMsg.LOGCAT, 200); } } } }</code></pre>

页面列表

ITEM_HTML