Search This Blog

07 July 2010

Error reporting via email

Let's start with a basic app:
C:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
Choose a number:  (1/2/3): 2
Confirm properties configuration:
groupId: org.eoti.android.errors
artifactId: ErrorTest
version: 1.0-SNAPSHOT
package: org.eoti.android.errors
C:\work> cd ErrorTest
C:\work\ErrorTest> mvn clean install

Insert this into the end of your onCreate method:
[* NOTE: Make sure to change to your email address if you want to see it actually work ]
        try{
            throw new IOException("This is an error");
        } catch (IOException e) {
            Log.e(TAG, "Standard Error Logging", e);

            // Derived from http://thinkandroid.wordpress.com/2010/01/25/debugging-applications-by-emailing-error-reports/
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"malachid@gmail.com"});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "[" + getClass().getSimpleName() + "] ERROR REPORT");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, sw.toString());
            startActivity(Intent.createChooser(emailIntent, "Send error report..."));
        }

Redeploy (mvn clean install).and run it.  You should soon have an email show the stack trace.

1 comment:

  1. To use in the emulator, you will need to configure an email client. If you are using a gmail or google apps account, you can find the correct settings here.

    ReplyDelete