And You Thought Spam Was Irritating
You can keep track of exceptions users are getting by adding a method to a "Utils
" class in your app like so:
Note: I adapted this code from here.
public void sendEmail(String emailAddress, String emailSubject, String emailBody) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {emailAddress});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
startActivity(Intent.createChooser(emailIntent, "Sending email..."));
}
In context (you will need the context (no pun inten[d,t]ed) to call startActivity
):
public class MyBeaudaciousUtils {
private Context _context;
public String EMAIL_ADDRESS = "johnny_appleseed@orchardsRUs.com";
public void sendEmail(String emailAddress, String emailSubject, String emailBody) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/html");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {emailAddress});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
_context.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
}
. . .
Now you can call sendEmail
from anywhere in your project:
MyBeaudaciousUtils.sendEmail("sirspamalot@quixote.net",
"something", "<p>Yikes! Tykes on Trikes!</p>");
Exceptional Email
You might want to put the call to sendEmail()
inside a catch
block, like so:
} catch (Exception e) {
String errHTML = String.format("<h2>EXCEPTIONAL!</h2><p>%s</p>", e.getMessage());
hhsUtils.sendEmail(hhsUtils.EMAIL_ADDRESS, "Exception in GetAJob()", errHTML);
}
...or, if your catch
block is inside a method that returns a String
value, and you also want to write the exception to the debug console, you can do this:
} catch (Exception e) {
System.out.println(e.getMessage());
String errHTML = String.format("<h2>EXCEPTIONAL!</h2><p>%s</p>", e.getMessage());
hhsUtils.sendEmail(hhsUtils.EMAIL_ADDRESS, "Exception in GetURLToUse()", errHTML);
return e.getMessage();
}
You (or the owner of the email address shown above) will then get an email that looks something like the following following an exception:
EXCEPTIONAL!
Too many vestigial spider monkeys playing rosin-less-bowed violins have escaped into the innards of your code!
YMMV
It's possible the exception message you get will be more informative (but more boring) than the example shown above (Your Monkeys May Vary).
aELttR
"aElttR" is not just a near-anagram of "A Letter"; it stands for, "an Exercise Left to the Reader" which is: you may want to add more info to the email you send to yourself (or to whomever is to be the recipient of the exception messages), such as: username and/or location, version of the software they are running, a time stamp, the specific type of device they are using, or any other tidbit that will help you in your debugging/spelunking/code detective efforts.