2011-11-04 18 views
1

我剛剛起步的前30行,我如何可以查看新線在我的應用程序生成,這裏是我的代碼:如何看待連續logcat的在我的應用程序在模擬器

package com.example.showinlog; 


public class ShowingLog extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try { 
      Process process = Runtime.getRuntime().exec("logcat"); 
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream())); 

      StringBuilder log=new StringBuilder(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 

       log.append(line); 
       log.append("\n"); 

      } 
      TextView tv = (TextView)findViewById(R.id.textView1); 
      tv.setText(log.toString()); 
      } catch (IOException e) { 
      } 
    } 
} 

回答

2

我實際上不知道你如何得到任何東西。閱讀不應該「結束」,因爲你不會在不同的線程中閱讀,所以你不應該到達初始化TextView的部分。

即使你確實已經達到了可以不斷記錄文本的程度,但它不適用於這段代碼,因爲你永遠不會「完成」構建你的StringBuilder。

試試這個。你需要在LogcatOut通過作爲日誌數據的回調:

public class LolCat 
{ 
    private Process proc; 
    private LogcatOut logcatOut; 

    public LolCat(LogcatOut logcatOut) 
    { 
     this.logcatOut = logcatOut; 
    } 

    private InputStream inStd; 

    private InputStream inErr; 

    private LogcatProcessStreamReader streamReader; 
    private LogcatProcessStreamReader errStreamReader; 

    public void start() 
    { 
     try 
     { 
      proc = Runtime.getRuntime().exec("logcat"); 
      OutputStream os = proc.getOutputStream(); 

      this.inStd = proc.getInputStream(); 
      this.inErr = proc.getErrorStream(); 

      startReaders(); 

      os.flush(); 
     } 
     catch (IOException e) 
     { 
//   App.logExecption("Can't logcat", e); 
     } 
     catch (Exception e1) 
     { 
//   App.logExecption("Can't logcata", e1); 
     } 
    } 

    private void startReaders() throws FileNotFoundException 
    { 
     this.streamReader = new LogcatProcessStreamReader(this.inStd, logcatOut); 
     this.errStreamReader = new LogcatProcessStreamReader(this.inErr, null); 

     streamReader.start(); 
     errStreamReader.start(); 
    } 

    public void kill() 
    { 
     proc.destroy(); 
     if (this.streamReader != null) 
      this.streamReader.finish(); 
     if (this.errStreamReader != null) 
      this.errStreamReader.finish(); 
    } 

    public abstract class LogcatOut 
    { 
     public abstract void writeLogData(byte[] data, int read) throws IOException; 
     protected void cleanUp() 
     { 

     } 
    } 

    class LogcatProcessStreamReader extends Thread 
    { 
     private InputStream in; 
     private boolean done = false; 
     private LogcatOut logcatOut; 

     public LogcatProcessStreamReader(InputStream in, LogcatOut logcatOut) 
     { 
      this.in = in; 
      this.logcatOut = logcatOut; 
     } 

     @Override 
     public void run() 
     { 
      byte[] b = new byte[8 * 1024]; 
      int read; 

      try 
      { 
       while (!done && ((read = in.read(b)) != -1)) 
       { 
        if(logcatOut != null) 
         logcatOut.writeLogData(b, read); 
       } 

       if(logcatOut != null) 
        logcatOut.cleanUp(); 
      } 
      catch (IOException e) 
      { 
//    App.logExecption("Can't stream", e); 
      } 
     } 

     public synchronized void finish() 
     { 
      done = true; 
     } 
    } 
} 

在你的onCreate:

final Handler handler = new Handler(); 
    new LolCat(new LolCat.LogcatOut() 
    { 
     @Override 
     public void writeLogData(final byte[] data, final int read) throws IOException 
     { 
      handler.post(new Runnable() 
      { 
       public void run() 
       { 
        TextView tv = (TextView) asdf; 
        tv.setText(tv.getText() + "\n" + new String(data, 0, read)); 

       } 
      }); 
     } 
    }); 

幾個注意事項:

1)我適應這個從其他我有的代碼。我沒有測試過它。您可能會遇到空指針異常或類似情況,但基本代碼應該可以工作。

2)你需要做日誌的權限(忘了是什麼)

3)如果日誌數據來源於STD出來,我不記得或犯錯了。我認爲它的標準,但如果你什麼也沒有,交換。

4)我不會推薦像在這裏在文本視圖中那樣對文本進行排序。你需要實現一個可以被限制的緩衝區,而且在Java中大字符串連接顯然是不好的。我將這個解決方案留給讀者...

2

我發現AsyncTasks在嘗試實現這個時非常有用。

public class LogCatTask extends AsyncTask<Void, String, Void> {  
    public AtomicBoolean run = new AtomicBoolean(true); 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      Runtime.getRuntime().exec("logcat -c"); 
      Process process = Runtime.getRuntime().exec("logcat"); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
      StringBuilder log = new StringBuilder(); 
      String line = ""; 
      while (run.get()) { 
       line = bufferedReader.readLine(); 
       if (line != null) { 
        log.append(line); 
        publishProgress(log.toString()); 
       } 
       line = null; 
       Thread.sleep(10); 
      } 
     } 
     catch(Exception ex){ 

     } 
     return null; 
    } 
} 

而且實現你這樣做

public void setupTextView(){ 
    textView.setMovementMethod(new ScrollingMovementMethod()); 
    logCatTask = new LogCatTask(){ 
     @Override 
     protected void onProgressUpdate(String... values) { 
      textView.setText(values[0]); 
      super.onProgressUpdate(values); 
     } 
    }; 
    logCatTask.execute(); 
} 
+0

應該提到阻止你只需要logCatTask.run.set(假)的任務的任務; –

相關問題