Skip to content

Debugging Applications by Emailing Error Reports

January 25, 2010

Hey everyone,

Just thought I’d share something nifty that I used in one of my applications and which has come in very handy. This little code snippet/example is for anyone who has been frustrated because their application works fine on their emulators / development phones, but who are getting reports from users on other phones across other versions of Android experiencing issues.

Now, because you can’t debug the issue and don’t want to stab at it blindly, you could try doing something like this:

public class ExampleActivity extends Activity {

    @Override
    protected void onCreate() {
         // catch any exceptions     
        try {
            // do your stuff here
         } catch (Exception e) {
            // get the stack trace as a string
             StringWriter sw = new StringWriter();
             e.printStackTrace(new PrintWriter(sw));
             String stacktrace = sw.toString();

             // create an email intent to send to yourself
             final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
             emailIntent.setType("plain/text");
             emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "lets.think.android@gmail.com" });
             emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "App Error Report");
             emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, stacktrace);

             // start the email activity - note you need to start it with a chooser
             startActivity(Intent.createChooser(emailIntent, "Send error report..."));
         }
    }
}

So basically, what you can do is wrap any “suspicious” parts of your code in a try catch block, and when you catch the exception turn it into a string and then start basically a Bug Report activity which takes the form of an email. The user then simply just needs to click SEND and you’ll immediately get an email with the stack trace and voila!

Again, this has come in especially handy for me and is something I thought I’d share.

Happy coding.

– jwei

Advertisements
3 Comments leave one →
  1. Jeffrey permalink
    March 1, 2010 1:07 pm

    Thank you for the code snippet, great idea.

  2. Hyung Goo Kim permalink
    December 20, 2011 6:37 am

    Wow. I think it’s very useful. Thank you

  3. Memings permalink
    September 10, 2012 8:34 am

    Thank you very much for the code, really helped me a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: