Search This Blog

07 July 2010

Error reporting via email

Let's start with a basic app:
C:\work> mvn archetype:generate -DarchetypeCatalog=
2: -> galatea-archetype (null)
Choose a number:  (1/2/3): 2
Confirm properties configuration:
artifactId: ErrorTest
version: 1.0-SNAPSHOT
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 ]
            throw new IOException("This is an error");
        } catch (IOException e) {
            Log.e(TAG, "Standard Error Logging", e);

            // Derived from
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{""});
            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.