Friday, March 23, 2012

How to read Logcat contents programmatically in Android?


How to read Logcat contents programmatically in Android?

This is an example to read Logcat contents programmatically in Android.
Normal developers won’t need this, but still it is a good thing to know how to do it.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package pack.coderzheaven;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public 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) {
            }
          }
}
Read Logcat Demo
Read Logcat Demo
if you like the post then click on the plus button to share it with your friends and leave your valuable comments.

No comments: