Wednesday, March 9, 2011

Access LOGCAT DATA in your ANDROID application Progrmatically.

AIM: 
To read the LOGCAT data in your application which is very helpful  for bugreport purposes and for trobleshooting the device issues from remote location.


Solution:

You can check LOGCAT API documentation at  developer site.

Writing program to use that API and to print your logcat data in a Scrollable TextView.

1)
     public class ReadLogCat extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
          Process process = Runtime.getRuntime().exec("logcat -v");// Verbose filter
/*  Check diffrent filters available at http://developer.android.com/guide/developing/tools/logcat.html , Options section */

          BufferedReader bufferedReader = new BufferedReader(
          new InputStreamReader(process.getInputStream()));
                          
          StringBuilder logString=new StringBuilder();
          String line;
          while ((line = bufferedReader.readLine()) != null) {
            logString.append(line);
          }
          TextView tv = (TextView)findViewById(R.id.logTextView);
          tv.setText(logString.toString());
        } catch (IOException e) {
        }
      }
    }

2) To perform this operation we need to set Permissions in AndroidManifest.xml file as 

<uses-permission android:name="android.permission.READ_LOGS" />

3) The following is the LOGCAT window in ECLIPSE IDE.


Logcat window in ECLIPSE
 Post your comments if any .




3 comments:

  1. "logcat -v" But the documentation says this will print everything as per Priority ...

    Check this link instead
    http://developer.android.com/guide/developing/debugging/debugging-log.html#outputFormat

    ReplyDelete
  2. Thanks.
    I used "logcat -d -v time -s " + tag name.
    You need the -d by the way, otherwise this process stays up in the background!

    ReplyDelete
  3. How can we see the logcat data for an app already on the phone? Please let me know.

    ReplyDelete

Android Developers Blog

Ram's shared items