개Foot/Dog발?!
2014. 7. 26. 23:29
앞의 코드에 이어서
내가 테스트용으로 잠깐 만들었던
주요 코드를 적어본다.
@Override public void run() { // TODO Auto-generated method stub final String result = getLog(this); runOnUiThread(new Runnable() { @Override public void run() { try { if (testView != null) { testView.setText(result); } } catch (Exception e) { e.printStackTrace(); } } }); runOnUiThread(new Runnable() { @Override public void run() { try { if (testButton != null) { testButton.setText("START LOG"); } if (harvestLog != null) { harvestLog.setVisibility(View.GONE); harvestLog.incrementProgressBy(0); } if (testView != null) { testView.setVisibility(View.VISIBLE); } } catch (Exception e) { e.printStackTrace(); } } }); } public final String getLog(Context context) { String[] LOGCAT_CMD = new String[] { "logcat", "-d", "*:V" }; Process logcatProc = null; try { logcatProc = Runtime.getRuntime().exec(LOGCAT_CMD); } catch (IOException e) { e.printStackTrace(); return ""; }
BufferedReader reader = null;
String lineSeparatoer = System.getProperty("line.separator");
StringBuilder strOutput = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
String line;
while ((line = reader.readLine()) != null) { strOutput.append(line); strOutput.append(lineSeparatoer); if(FORCE_BREAK){ break; } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) {} } logcatProc.destroy(); logcatProc = null; }
FORCE_BREAK = false; return strOutput.toString(); } |