Can logcat results for Log.i be viewed in our activity?

廉价感情. 提交于 2019-12-20 02:09:50

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/7863841/can-logcat-results-for-log-i-be-viewed-in-our-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!