可以在我们的活动中查看Log.i的logcat结果吗?

jeh*_*han 3 android android-logcat

我想在我的应用程序中显示Log.i结果.可能吗?如果是这样,我该怎么办?

Kur*_*aum 11

这是一个博客文章,完全符合您的需要.它有一个完整的代码示例,介绍如何显示Logcat日志的内容.这是代码:

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;

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

              StringBuilder log=new StringBuilder();
              String line = ""; 
              while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
              }   
              TextView tv = (TextView)findViewById(R.id.textView1);
              tv.setText(log.toString());
            } catch (IOException e) {
          }
        }
} 
Run Code Online (Sandbox Code Playgroud)