title: Need to handle uncaught exception and send log file
body: <p>When my app creates an unhandled exception, rather than simply terminating, I'd like to first give the user an opportunity to send a log file. I realize that doing more work after getting a random exception is risky but, hey, the worst is the app finishes crashing and the log file doesn't get sent. This is turning out to be trickier than I expected :)</p>
  <p>What works: (1) trapping the uncaught exception, (2) extracting log info and writing to a file.</p>
  <p>What doesn't work yet: (3) starting an activity to send email. Ultimately, I'll have yet another activity to ask the user's permission. If I get the email activity working, I don't expect much trouble for the other.</p>
  <p>The crux of the problem is that the unhandled exception is caught in my Application class. Since that isn't an Activity, it's not obvious how to start an activity with Intent.ACTION_SEND. That is, normally to start an activity one calls startActivity and resumes with onActivityResult. These methods are supported by Activity but not by Application.</p>
  <p>Any suggestions on how to do this?</p>
  <p>Here are some code snips as a starting guide:</p>
  <pre><code>public class MyApplication extends Application
  {
    defaultUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler();
    public void onCreate ()
    {
      Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
      {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
          handleUncaughtException (thread, e);
        }
      });
    }
  
    private void handleUncaughtException (Thread thread, Throwable e)
    {
      String fullFileName = extractLogToFile(); // code not shown
  
      // The following shows what I'd like, though it won't work like this.
      Intent intent = new Intent (Intent.ACTION_SEND);
      intent.setType (&quot;plain/text&quot;);
      intent.putExtra (Intent.EXTRA_EMAIL, new String[] {&quot;me@mydomain.example&quot;});
      intent.putExtra (Intent.EXTRA_SUBJECT, &quot;log file&quot;);
      intent.putExtra (Intent.EXTRA_STREAM, Uri.parse (&quot;file://&quot; + fullFileName));
      startActivityForResult (intent, ACTIVITY_REQUEST_SEND_LOG);
    }
  
    public void onActivityResult (int requestCode, int resultCode, Intent data)
    {
      if (requestCode == ACTIVITY_REQUEST_SEND_LOG)
        System.exit(1);
    }
  }
  </code></pre>