/** * Print report from a single command line. * <p> * TODO: Use ProcessBuilder & redirectErrorStream(true) to capture both * streams (might be important for some command lines) * 通过执行命令,然后通过输出流进行显示 * * @param reportName Simple tag that will print before the report and in * various annotations. * @param command Command line to execute. */ privatevoidcommandLineReport(String reportName, String command) { Logger.err.println(reportName + ":"); Runtimert= Runtime.getRuntime(); WriterlogOutput=null; try { // Process must be fully qualified here because android.os.Process // is used elsewhere java.lang.Processp= Runtime.getRuntime().exec(command); if (mRequestBugreport) { logOutput = newBufferedWriter(newFileWriter(newFile(Environment .getLegacyExternalStorageDirectory(), reportName), true)); } // pipe everything from process stdout -> System.err InputStreaminStream= p.getInputStream(); InputStreamReaderinReader=newInputStreamReader(inStream); BufferedReaderinBuffer=newBufferedReader(inReader); String s; while ((s = inBuffer.readLine()) != null) { // 如果命令行参数有设置--bugreport,则会把文件输出到手机的存储目录上; // 如果没设置的话,则会通过日志打印出来 if (mRequestBugreport) { try { // When no space left on the device the write will // occurs an I/O exception, so we needed to catch it // and continue to read the data of the sync pipe to // aviod the bugreport hang forever. 发现错别字 aviod -> avoid logOutput.write(s); logOutput.write("\n"); } catch (IOException e) { while(inBuffer.readLine() != null) {} Logger.err.println(e.toString()); break; } } else { Logger.err.println(s); } } intstatus= p.waitFor(); Logger.err.println("// " + reportName + " status was " + status); if (logOutput != null) { logOutput.close(); } } catch (Exception e) { Logger.err.println("// Exception from " + reportName + ":"); Logger.err.println(e.toString()); } }
reportDumpsysMemInfo 也类似,只是执行的命令不一样而已,这里不再赘述。
1 2 3 4 5 6 7 8 9 10
/** * Run "dumpsys meminfo" * <p> * NOTE: You cannot perform a dumpsys call from the ActivityController * callback, as it will deadlock. This should only be called from the main * loop of the monkey. */ privatevoidreportDumpsysMemInfo() { commandLineReport("meminfo", "dumpsys meminfo"); }