本文介绍了可以logcat的结果Log.i在我们的活动中查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to display Log.i results in my application. Is it possible? If so, how can I do it?

推荐答案

下面是一个<一href="http://$c$crzheaven.com/2011/07/how-to-read-logcat-contents-programmatically-in-android/">blogpost这不正是你需要做的事。它对如何显示的logcat日志的内容完整code例子。这里的code:

Here's a blogpost that does exactly what you need it to do. It has a complete code example on how to display the contents of the Logcat log. Here's the code:

  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) {
          }
        }
} 

这篇关于可以logcat的结果Log.i在我们的活动中查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:54